Is there a less verbose alternative to this:
for x in xrange(array.shape[0]):
for y in xrange(array.shape[1]):
do_stuff(x, y)
I came up with this:
for x, y in itertools.product(map(xrange, array.shape)):
do_stuff(x, y)
Which saves one indentation, but is still pretty ugly.
I’m hoping for something that looks like this pseudocode:
for x, y in array.indices:
do_stuff(x, y)
Does anything like that exist?
I think you’re looking for the ndenumerate.
Regarding the performance. It is a bit slower than a list comprehension.
If you are worried about the performance you could optimise a bit further by looking at the implementation of
ndenumerate, which does 2 things, converting to an array and looping. If you know you have an array, you can call the.coordsattribute of the flat iterator.