[Numpy-discussion] Slice specified axis
Tony Yu
tsyu80 at gmail.com
Thu Apr 5 15:52:03 EDT 2012
Is there a way to slice an nd-array along a specified axis? It's easy to
slice along a fixed axis, e.g.:
axis = 0:
>>> array[start:end]
axis = 1:
>>> array[:, start:end]
...
But I need to do this inside of a function that accepts arrays of any
dimension, and the user can operate on any axis of the array. My current
solution looks like the following:
>>> aslice = lambda axis, s, e: (slice(None),) * axis + (slice(s, e),)
>>> array[aslice(axis, start, end)]
which works, but I'm guessing that numpy has a more readable way of doing
this that I've overlooked.
Thanks,
-Tony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120405/27af31b0/attachment.html>
More information about the NumPy-Discussion
mailing list