I have created a multidimensional array in Python like this:
self.cells = np.empty((r,c),dtype=np.object)
Now I want to iterate through all elements of my twodimensional array, and I do not care about the order. How do I achieve this?
I have created a multidimensional array in Python like this:
Now I want to iterate through all elements of my twodimensional array, and I do not care about the order. How do I achieve this? |
|||
|
It's clear you're using numpy. With numpy you can just do:
|
|||
|
If you need to change the values of the individual cells then ndenumerate (in numpy) is your friend. Even if you don't it probably still is!
|
|||
|
Just iterate over one dimension, then the other.
Of course, with only two dimensions, you can compress this down to a single loop using a list comprehension or generator expression, but that's not very scalable or readable:
If you need to scale this to multiple dimensions and really want a flat list, you can write a |
|||||||||||||||||
|
How about this:
|
|||||
|