Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python numpy.ndenumerate是否以Fortran顺序返回索引?_Python_Arrays_Numpy_Iterator_Iteration - Fatal编程技术网

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()