有没有那么冗长的替代方案: 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) 有没有类似的东西存在?
有没有那么冗长的替代方案:
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
我认为您正在寻找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
关于性能。它比列表理解要慢一些。
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
如果您担心性能,可以通过查看实现来进一步优化ndenumerate
,它实现了两件事,转换为数组并循环。如果知道有数组,则可以调用.coords
平面迭代器的属性。
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
如果只需要索引,可以尝试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)]
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
见nditer
import numpy as np
Y = np.array([3,4,5,6])
for y in np.nditer(Y, op_flags=['readwrite']):
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])
for y in np.nditer(Y, op_flags=['readwrite']):
y += 3
Y == np.array([6, 7, 8, 9])
y = 3
would not work, use y *= 0
and y += 3
instead.
本文由 Python 实用宝典 作者:Python实用宝典 发表,其版权均为 Python 实用宝典 所有,文章内容系作者个人观点,不代表 Python 实用宝典 对观点赞同或支持。如需转载,请注明文章来源。