Python numpy.ndenumerate是否以Fortran顺序返回索引?
使用Python numpy.ndenumerate是否以Fortran顺序返回索引?,python,arrays,numpy,iterator,iteration,Python,Arrays,Numpy,Iterator,Iteration,使用numpy.ndenumerate时,对于C-连续数组顺序,将返回以下索引,例如: import numpy as np a = np.array([[11, 12], [21, 22], [31, 32]]) for (i,j),v in np.ndenumerate(a): print i, j, v 如果a中的顺序为'F'或'C',则无数学关系,这将给出: 0 0 11 0 1 12 1 0 21 1 1 22 2 0
numpy.ndenumerate
时,对于C-连续
数组顺序,将返回以下索引,例如:
import numpy as np
a = np.array([[11, 12],
[21, 22],
[31, 32]])
for (i,j),v in np.ndenumerate(a):
print i, j, v
如果a
中的顺序
为'F'
或'C'
,则无数学关系,这将给出:
0 0 11
0 1 12
1 0 21
1 1 22
2 0 31
2 1 32
在numpy
中是否有类似于ndenumerate
的内置迭代器(在数组order='F'
之后)给出此值:
只需进行转置即可满足您的需求:
a = np.array([[11, 12],
[21, 22],
[31, 32]])
for (i,j),v in np.ndenumerate(a.T):
print j, i, v
结果:
0 0 11
1 0 21
2 0 31
0 1 12
1 1 22
2 1 32
您可以通过以下方式执行此操作:
it = np.nditer(a, flags=['multi_index'], order='F')
while not it.finished:
print it.multi_index, it[0]
it.iternext()
np.nditer
是一个非常强大的野兽,它在Python中公开了一些内部C迭代器,请查看文档。是的,这肯定更好:)@Akavall太棒了!哎呀!刚刚在循环中添加了it.iternext()
,以使迭代向前推进……我认为对于其中的项也有效。我不清楚为什么文档选择这种特殊的方式来推进迭代器。是吗?一点线索也没有。我想直到今天我才尝试在Python中使用nditer
。。。
it = np.nditer(a, flags=['multi_index'], order='F')
while not it.finished:
print it.multi_index, it[0]
it.iternext()