2019-01-24
阅读量:
735
python如何控制迭代的顺序
有时以特定顺序访问数组元素很重要,无论内存中元素的布局如何。nditer对象提供了一个order参数来控制迭代的这个方面。具有上述行为的默认值是order ='K'以保持现有订单。对于C顺序,可以使用order ='C'覆盖它,对于Fortran顺序,可以使用order ='F'覆盖它。
代码#1:
# Python program for# iterating over array# using particular order
import numpy as geek
# creating an array using arrange # methoda = geek.arange(12)
# shape array with 3 rows and # 4 columns a = a.reshape(3,4)
print('Original array is:') print(a)print()
print('Modified array in C-style order:')
# iterating an array in a given# order for x in geek.nditer(a, order = 'C'):
print(x)在IDE上运行
输出:
原始数组是:
[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
C风格的修改数组:
0 1 2 3 4 5 6 7 8 9 10 11
46.1538
1
3
关注作者
收藏
评论(0)
发表评论
暂无数据
推荐帖子
0条评论
0条评论
0条评论

