NumPy - 遍历数组

NumPy - 遍历数组 首页 / Numpy入门教程 / 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,

它的输出将是-

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-iterating-over-array.html

来源:LearnFk无涯教程网

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

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.Parameter & 描述
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]

广播迭代

如果两个数组可broadcasting广播,则组合的 nditer对象可以同时对其进行迭代。假设数组 a 的维度为3X4,并且还有另一个数组 b 的维度为1X4,则使用以下类型的迭代器(数组 b broadcasting为 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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Python核心技术与实战 -〔景霄〕

后端技术面试 38 讲 -〔李智慧〕

Linux内核技术实战课 -〔邵亚方〕

实用密码学 -〔范学雷〕

郭东白的架构课 -〔郭东白〕

深入浅出分布式技术原理 -〔陈现麟〕

李智慧 · 高并发架构实战课 -〔李智慧〕

云计算的必修小课 -〔吕蕴偲〕

程序员职业规划手册 -〔雪梅〕

好记忆不如烂笔头。留下您的足迹吧 :)