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,

其输出如下-

链接: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]]

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

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

其输出如下-

链接: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]]

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),

其输出如下-

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

来源:LearnFk无涯教程网

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

祝学习愉快! (发现内容有误?请选中要编辑的内容 -> 右键 -> 修改 -> 提交!帮助我们改进教程质量)

精选教程推荐

👇 以下精选教程可能对您有帮助,拓展您的技术视野

结构沟通力 -〔李忠秋〕

后端工程师的高阶面经 -〔邓明〕

零基础GPT应用入门课 -〔林健(键盘)〕

手把手带你搭建秒杀系统 -〔佘志东〕

跟月影学可视化 -〔月影〕

Electron开发实战 -〔邓耀龙〕

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

DevOps实战笔记 -〔石雪峰〕

Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕

📝 好记忆不如烂笔头,留下您的学习笔记吧!

暂无学习笔记,成为第一个分享的人吧!

您的笔记将帮助成千上万的学习者