循环遍历ndarray的所有元素

时间:2018-05-23 18:24:02

标签: python numpy for-loop

In [6]: a = np.array([[1,2,3,4],[5,6,7,8]])

In [7]: b = a

In [8]: a[0]
Out[8]: array([1, 2, 3, 4])

In [9]: a[0][0]
Out[9]: 1

但我想使用zip并循环浏览ab并获取a[0][0]后跟a[0][1],直到我达到{{1} }}。

当我尝试以下操作时:

a[1][3]

我希望In [11]: for i,j in zip(a,b): ...: print i[0][0] ...: --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-11-8a9c71fab781> in <module>() 1 for i,j in zip(a,b): ----> 2 print i[0][0] 3 IndexError: invalid index to scalar variable. 后跟a[0][0] = 1最多a[0][1] = 2,然后获取a[0][3] = 4,依此类推a[1][0] = 5

5 个答案:

答案 0 :(得分:5)

如果只是需要逐个遍历a的所有元素,那就是ndarray.flat的用途:

In [11]: a = np.array([[1,2,3,4],[5,6,7,8]])

In [13]: for i in a.flat: print(i)
1
2
3
4
5
6
7
8

循环a.flatten()会产生相同的结果,但它会构建一个单独的ndarray并复制数据。

.flat也比itertools.chain.from_iterable(a)更有效,因为后者涉及从a获取完整行作为视图,然后迭代它们 - 即许多额外操作。

如果您在迭代时需要知道索引,请按照numpy.ndenumerate使用Iterating over a numpy array

In [34]: for (x,y),i in np.ndenumerate(a): print("a[%d][%d]=%d"%(x,y,i))
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[0][3]=4
a[1][0]=5
a[1][1]=6
a[1][2]=7
a[1][3]=8

答案 1 :(得分:3)

获取压缩和索引的一种方法是使用np.nditer

>>> a = np.arange(1,9).reshape(2,4)
>>> b = -a
>>> 
>>> it = np.nditer((a, b), order='C', flags=('multi_index',))
>>> for i, j in it:
...     print(it.multi_index, i, j)
... 
(0, 0) 1 -1
(0, 1) 2 -2
(0, 2) 3 -3
(0, 3) 4 -4
(1, 0) 5 -5
(1, 1) 6 -6
(1, 2) 7 -7
(1, 3) 8 -8

作为免费奖励,这适用于广播:

>>> a, b = np.ogrid[1:3, 2:6]
>>> a.shape, b.shape
((2, 1), (1, 4))
>>> 
>>> it = np.nditer((a, b), order='C', flags=('multi_index',))
>>> for i, j in it:
...     print(it.multi_index, i, j)
... 
(0, 0) 1 2
(0, 1) 1 3
(0, 2) 1 4
(0, 3) 1 5
(1, 0) 2 2
(1, 1) 2 3
(1, 2) 2 4
(1, 3) 2 5

答案 2 :(得分:2)

In [333]: a = np.array([[1,2,3,4],[5,6,7,8]])
In [334]: a
Out[334]: 
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

获取价值及其指数的便捷方法是ndenumerate

In [335]: np.ndenumerate(a)
Out[335]: <numpy.lib.index_tricks.ndenumerate at 0x7f39160b5160>
In [336]: list(_)
Out[336]: 
[((0, 0), 1),
 ((0, 1), 2),
 ((0, 2), 3),
 ((0, 3), 4),
 ((1, 0), 5),
 ((1, 1), 6),
 ((1, 2), 7),
 ((1, 3), 8)]

它会创建一个iter = a.flat一个flatiter对象,然后对其进行迭代,返回coordsnext

如果您只想要值,而不是坐标

In [19]: list(a.flat)
Out[19]: [1, 2, 3, 4, 5, 6, 7, 8]

ndindex可用于为给定形状生成索引:

In [20]: idx=np.ndindex(a.shape)
In [21]: [(ij, a[ij]) for ij in idx]
Out[21]: 
[((0, 0), 1),
 ((0, 1), 2),
 ((0, 2), 3),
 ((0, 3), 4),
 ((1, 0), 5),
 ((1, 1), 6),
 ((1, 2), 7),
 ((1, 3), 8)]

答案 3 :(得分:1)

从您想要的输出public class LibraryRequest { private LibraryProfile libraryProfile; @XmlElement(name = "libraryProfile") public LibraryProfile getLibraryProfile(){ ... } // setters public class LibraryProfile { // constructors, getters & setters for primitive types private List<BookInfo> bookInfos; public List<BookInfo> getBookInfo(){ return this.BookInfos; } // rest of the functions ,您可以使用1, 2, ..., 4, 5, ..., 8来迭代值:

itertools.chain

答案 4 :(得分:1)

我会试试这个

a = np.array([[1,2,3,4],[5,6,7,8]])
for row in [0,1]:
    for column in range(4):
        print('a[%s][%s] = %s' % (row, column, a[row][column]))

打印:

a[0][0] = 1
a[0][1] = 2
a[0][2] = 3
a[0][3] = 4
a[1][0] = 5
a[1][1] = 6
a[1][2] = 7
a[1][3] = 8