用简单的方式控制迭代比较容易
for:
In [17]: aOut[17]: array([[0, 1, 2], [3, 4, 5]])In [18]: for row in a: ...: print(row) ...: [0 1 2][3 4 5]
这样做
nditer很尴尬。除非您需要
cython如页面结尾所述使用广播,否则
nditer不会提供任何速度优势。即使有了
cython,我的速度也
memoryviews超过了
nditer。
看看
np.ndindex。它创建一个尺寸减小的虚拟数组,并对此进行nditer:
In [20]: for i in np.ndindex(a.shape[0]): ...: print(a[i,:]) ...: [[0 1 2]][[3 4 5]]
得到它了:
In [31]: for x in np.nditer(a.T.copy(), flags=['external_loop'], order='F'): ...: print(x)[0 1 2][3 4 5]
就像我说的-尴尬
我最近探讨了在一维结构化数组上直接迭代与nditer之间的区别:https
://stackoverflow.com/a/43005985/901925



