# 遍历一个numpy数组

## 问题：遍历一个numpy数组

``````for x in xrange(array.shape[0]):
for y in xrange(array.shape[1]):
do_stuff(x, y)``````

``````for x, y in itertools.product(map(xrange, array.shape)):
do_stuff(x, y)``````

``````for x, y in array.indices:
do_stuff(x, y)``````

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?

## 回答 0

``````>>> a =numpy.array([[1,2],[3,4],[5,6]])
>>> for (x,y), value in numpy.ndenumerate(a):
...  print x,y
...
0 0
0 1
1 0
1 1
2 0
2 1``````

``````X = np.zeros((100, 100, 100))

%timeit list([((i,j,k), X[i,j,k]) for i in range(X.shape[0]) for j in range(X.shape[1]) for k in range(X.shape[2])])
1 loop, best of 3: 376 ms per loop

%timeit list(np.ndenumerate(X))
1 loop, best of 3: 570 ms per loop``````

``````a = X.flat
%timeit list([(a.coords, x) for x in a.flat])
1 loop, best of 3: 305 ms per loop``````

I think you're looking for the ndenumerate.

``````>>> a =numpy.array([[1,2],[3,4],[5,6]])
>>> for (x,y), value in numpy.ndenumerate(a):
...  print x,y
...
0 0
0 1
1 0
1 1
2 0
2 1
``````

Regarding the performance. It is a bit slower than a list comprehension.

``````X = np.zeros((100, 100, 100))

%timeit list([((i,j,k), X[i,j,k]) for i in range(X.shape[0]) for j in range(X.shape[1]) for k in range(X.shape[2])])
1 loop, best of 3: 376 ms per loop

%timeit list(np.ndenumerate(X))
1 loop, best of 3: 570 ms per loop
``````

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 `.coords` attribute of the flat iterator.

``````a = X.flat
%timeit list([(a.coords, x) for x in a.flat])
1 loop, best of 3: 305 ms per loop
``````

## 回答 1

``````>>> a = numpy.arange(9).reshape(3, 3)
>>> [(x, y) for x, y in numpy.ndindex(a.shape)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]``````

If you only need the indices, you could try `numpy.ndindex`:

``````>>> a = numpy.arange(9).reshape(3, 3)
>>> [(x, y) for x, y in numpy.ndindex(a.shape)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
``````

## 回答 2

``````import numpy as np
Y = np.array([3,4,5,6])
y += 3

Y == np.array([6, 7, 8, 9])``````

`y = 3`将无法使用`y *= 0``y += 3`而是使用。

see nditer

``````import numpy as np
Y = np.array([3,4,5,6])
`y = 3` would not work, use `y *= 0` and `y += 3` instead.