I have a three-dimensional ndarray in python and would like to iterate over it in an element-wise manner along two of the three margins.
Put more literally, I would for example like to iterate over all (x,y) pairs but keep the z data together as an array.
As pseudocode, the expression I ultimately am after would be something along these lines
[ f(z) for z in all_xy_pairs(the_ndarray) if g(z) == True ]
I considered using 'reshape' as follows
import numpy as np
# silly example
ii=np.arange(0,3*9,1).reshape(3,3,3)
[ z for z in ii.reshape(9,-1) if z[1]>10 ]
but I would prefer an iterator to which I could pass the array margins over which to iterate (in the example above margins=[0,1]. In pseudocode, the above example would then become
[ z for z in iterate_over_margins(ii, margins=[0,1]) if z[1]>10 ]
Before I start programming this myself, isn't there such an iterator in numpy or a related package? I checked nditer
but it doesn't do what I am after.