目录

NumPy - 迭代数组( Iterating Over Array)

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

让我们使用nditer ()函数创建一个3X4数组,并使用nditer迭代它。

例子1 (Example 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 (Example 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 (Example 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 (Example 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可选参数。 其默认值为只读,但可以设置为读写或只写模式。 这将允许使用此迭代器修改数组元素。

例子 (Example)

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'参数,它可以采用以下值 -

Sr.No. 参数和描述
1

c_index

可以跟踪C_order索引

2

f_index

追踪Fortran_order索引

3

multi-index

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

4

external_loop

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

例子 (Example)

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

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]

广播迭代

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

例子 (Example)

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
↑回到顶部↑
WIKI教程 @2018