NumPy 遍历数组


NumPy 包包含一个迭代器对象numpy.nditer,它是一个高效的多维迭代器对象,使用它可以迭代数组。使用 Python 的标准 Iterator 接口访问数组的每个元素。

让我们使用 arange() 函数创建一个 3X4 数组并使用nditer

示例 1

import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)

print 'Original array is:'
print a
print '\n'

print 'Modified array is:'
for x in np.nditer(a):
    print x,

这个程序的输出如下:

Original array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Modified array is:
0 5 10 15 20 25 30 35 40 45 50 55

示例 2

选择迭代顺序以匹配数组的内存布局,而不考虑特定的顺序,这可以通过迭代上述数组的转置来看到。

import numpy as np 
a = np.arange(0,60,5) 
a = a.reshape(3,4) 
   
print 'Original array is:'
print a 
print '\n'  
   
print 'Transpose of the original array is:' 
b = a.T 
print b 
print '\n'  
   
print 'Modified array is:' 
for x in np.nditer(b): 
    print x,

上述程序的输出如下:

Original array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Transpose of the original array is:
[[ 0 20 40]
 [ 5 25 45]
 [10 30 50]
 [15 35 55]]

Modified array is:
0 5 10 15 20 25 30 35 40 45 50 55

迭代顺序


如果使用 F 样式顺序存储相同的元素,则迭代器会选择更有效的数组迭代方式。

示例 1

import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'

print 'Transpose of the original array is:'
b = a.T
print b
print '\n'

print 'Sorted in C-style order:'
c = b.copy(order = 'C')
print c
for x in np.nditer(c):
    print x,

print '\n'

print 'Sorted in F-style order:'
c = b.copy(order = 'F')
print c
for x in np.nditer(c):
    print x,

它的输出如下:

Original array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Transpose of the original array is:
[[ 0 20 40]
 [ 5 25 45]
 [10 30 50]
 [15 35 55]]

Sorted in C-style order:
[[ 0 20 40]
 [ 5 25 45]
 [10 30 50]
 [15 35 55]]
0 20 40 5 25 45 10 30 50 15 35 55

Sorted in F-style order:
[[ 0 20 40]
 [ 5 25 45]
 [10 30 50]
 [15 35 55]]
0 5 10 15 20 25 30 35 40 45 50 55

示例 2

可以通过明确调用nditer来强制它使用一个特定的顺序。

import numpy as np 
a = np.arange(0,60,5) 
a = a.reshape(3,4) 

print 'Original array is:' 
print a 
print '\n'  

print 'Sorted in C-style order:' 
for x in np.nditer(a, order = 'C'): 
    print x,
print '\n' 

print 'Sorted in F-style order:' 
for x in np.nditer(a, order = 'F'): 
    print x,

它的输出将是:

Original array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Sorted in C-style order:
0 5 10 15 20 25 30 35 40 45 50 55

Sorted in F-style order:
0 20 40 5 25 45 10 30 50 15 35 55

修改数组值


nditer对象有另一个可选参数称为op_flags,其默认值为read-only,但可以设置为read-write或write-only模式,这将启用使用此迭代器修改数组元素。

import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'

for x in np.nditer(a, op_flags = ['readwrite']):
    x[...] = 2*x
print 'Modified array is:'
print a

其输出如下:

Original array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Modified array is:
[[ 0 10 20 30]
 [ 40 50 60 70]
 [ 80 90 100 110]]

外环


nditer 类构造函数有一个‘flags’参数,可以取以下值:

序号.参数及说明
1

c_index

C_order 索引可以跟踪

2

f_index

Fortran_order 索引被跟踪

3

multi-index

可以跟踪每次迭代一个索引的类型

4

external_loop

导致给定的值是具有多个值的一维数组而不是零维数组

在下面的例子中,迭代器遍历每一列对应的一维数组。

import numpy as np 
a = np.arange(0,60,5) 
a = a.reshape(3,4) 

print 'Original array is:' 
print a 
print '\n'  

print 'Modified array is:' 
for x in np.nditer(a, flags = ['external_loop'], order = 'F'):
    print x,

输出如下:

Original array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Modified array is:
[ 0 20 40] [ 5 25 45] [10 30 50] [15 35 55]

广播迭代


如果两个数组是可广播的,一个组合nditer对象能够同时迭代它们。假设一个数组a有维度 3X4,还有另一个数组b维度为 1X4,使用以下类型的迭代器(数组b广播到大小a).

import numpy as np 
a = np.arange(0,60,5) 
a = a.reshape(3,4) 

print 'First array is:' 
print a 
print '\n'  

print 'Second array is:' 
b = np.array([1, 2, 3, 4], dtype = int) 
print b  
print '\n' 

print 'Modified array is:' 
for x,y in np.nditer([a,b]): 
    print "%d:%d" % (x,y),

它的输出如下:

First array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Second array is:
[1 2 3 4]

Modified array is:
0:1 5:2 10:3 15:4 20:1 25:2 30:3 35:4 40:1 45:2 50:3 55:4