Say I have a 3D numpy.array, e.g. with dimensions x y z, is there a way to iterate over slices along a particular axis? Something like:
for layer in data.slices(dim=2):
# do something with layer
Edit:
To clarify, the example is a dim=3 array, i.e. shape=(len_x, len_y, len_z). Elazar and equivalently kamjagin's solutions work, but aren't that general - you have to construct the [:, :, i]
by hand, which means you need to know the dimensions, and the code isn't general enough to handle arrays of arbitrary dimensions. You can fill missing dimension by using something like [..., :]
, but again you still have to construct this yourself.
Sorry, should have been clearer, the example was a bit too simple!