Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through a multidimensional array in Python

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?

like image 711
utdiscant Avatar asked Jun 09 '09 18:06

utdiscant


People also ask

How do you iterate through a 2D array in Python?

Traversing in a 2D array in python can be done by using a for loop. We can iterate through the outer array first and then at each element of the outer array, we have another array which is our inner array containing the elements. So for each inner array, we run a loop to traverse its elements.

Does Python support multidimensional arrays?

In Python, Multidimensional Array can be implemented by fitting in a list function inside another list function, which is basically a nesting operation for the list function. Here, a list can have a number of values of any data type that are segregated by a delimiter like a comma.

Can NumPy arrays be multidimensional?

In general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.


1 Answers

It's clear you're using numpy. With numpy you can just do:

for cell in self.cells.flat:     do_somethin(cell) 
like image 144
tom10 Avatar answered Oct 14 '22 15:10

tom10