Try np.ndenumerate:
>>> a =numpy.array([[1,2],[3,4]])
>>> for (i,j), value in np.ndenumerate(a):
... print(i, j, value)
...
0 0 1
0 1 2
1 0 3
1 1 4
Answer from Lifu Huang on Stack OverflowLooping through each item in a numpy array? - Stack Overflow
python - How can I loop through 2D NumPy array using x and y coordinates without getting out of bounds error? - Stack Overflow
loops - Iterating over a 2 dimensional python list - Stack Overflow
Iterating through a numpy array
Try np.ndenumerate:
>>> a =numpy.array([[1,2],[3,4]])
>>> for (i,j), value in np.ndenumerate(a):
... print(i, j, value)
...
0 0 1
0 1 2
1 0 3
1 1 4
Make a small 2d array, and a nested list from it:
In [241]: A=np.arange(6).reshape(2,3)
In [242]: alist= A.tolist()
In [243]: alist
Out[243]: [[0, 1, 2], [3, 4, 5]]
One way of iterating on the list:
In [244]: for row in alist:
...: for item in row:
...: print(item)
...:
0
1
2
3
4
5
works just same for the array
In [245]: for row in A:
...: for item in row:
...: print(item)
...:
0
1
2
3
4
5
Now neither is good if you want to modify elements. But for crude iteration over all elements this works.
WIth the array I can easily treat it was a 1d
In [246]: [i for i in A.flat]
Out[246]: [0, 1, 2, 3, 4, 5]
I could also iterate with nested indices
In [247]: [A[i,j] for i in range(A.shape[0]) for j in range(A.shape[1])]
Out[247]: [0, 1, 2, 3, 4, 5]
In general it is better to work with arrays without iteration. I give these iteration examples to clearup some confusion.
You get prettier code with:
for iy, ix in np.ndindex(a.shape):
print(a[iy, ix])
resulting in:
1
2
3
4
5
6
7
8
9
10
11
12
a.shape[0] is the number of rows and the size of the first dimension, while a.shape[1] is the size of the second dimension. You need to write:
for x in range(0, rows):
for y in range(0, cols):
print a[x,y]
Note how rows and cols have been swapped in the range() function.
Edit: It has to be that way because an array can be rectangular (i.e. rows != cols). a.shape is the size of each dimension in the order they are indexed. Therefore if shape is (10, 5) when you write:
a[x, y]
the maximum x is 9 and the maximum y is 4.
x and y are actually poor names for array indices because they do not represent cartesian coordinates, but a location in memory. You can use i and j instead:
for i in range(0, rows):
for j in range(0, cols):
print a[i,j]
The documentation is a bit long but has a good, in-depth description of indices and shapes.
I know this is a dumb question, but I've scoured Google for the documentation, Stack Overflow, etc. but I can't figure out how to do this. I'm really used to C so I might be approaching this wrong.
Basically, I have a multidimensional array; specifically, it's n x 2. Another way of looking at it is that it's an array of tuples.
So suppose there are the points (1, 9), (2, 8), and (3, 7).
Now, my assumption is that [[1, 2, 3], [9, 8, 7]] is an nx2 array, and that [[1,9], [2, 8], [3, 7]] is a 2xn array, correct?
So my question is, in each case (given an nx2 and a 2xn array), how would I iterate through each ordered pair?