numpy迭代数组nditer、flat

nditer

NumPy 迭代器对象 numpy.nditer 提供了一种灵活访问一个或者多个数组元素的方式。
迭代器最基本的任务的可以完成对数组元素的访问。
np.nditer(op ,op_flags ,flags ,order,op_dtypes,casting ,op_axes , itershape ,buffersize )

import numpy as np
a = np.arange(0,60,5).reshape(3,4)
for one in np.nditer(a):
    print(one,end=', ')
#0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 

常用几个参数
op_flags: 修改数据的读写模式。默认情况下,nditer将待迭代的数组当成只读对象(readonly),为了在遍历数据时可以修改,需要修改成‘readwrite’或者‘writeonly’.注意op_flags是一个list

import numpy as np
a = np.arange(0,60,5).reshape(3,4)
for one in np.nditer(a,op_flags=['readwrite']):
    one[...] = one*2
print(a)
#[[ 0  5 10 15]
# [20 25 30 35]
# [40 45 50 55]]

#[[  0  10  20  30]
# [ 40  50  60  70]
# [ 80  90 100 110]]

flags:控制迭代器行为的标志.有如下值。

参数含义
c_index可以跟踪 C 顺序的索引
f_index可以跟踪 Fortran 顺序的索引
multi-index每次迭代可以跟踪tuple,tuple等于数据维度
external_loop给出的值是具有多个值的一维数组,而不是零维数组
#列优先索引
a = np.arange(0,60,5).reshape(4,3)
print(a)
it = np.nditer(a, flags=['f_index']) #列优先索引
print('数据 列优先索引')
while not it.finished:
    print('{} {}'.format(it[0],it.index),end='| ')
    it.iternext()
#[[ 0  5 10]
# [15 20 25]
# [30 35 40]
# [45 50 55]]
#数据 列优先索引
#0 0| 5 4| 10 8| 15 1| 20 5| 25 9| 30 2| 35 6| 40 10| 45 3| 50 7| 55 11

#行优先索引
a = np.arange(0,60,5).reshape(4,3)
print(a)
it = np.nditer(a, flags=['c_index'])#行优先索引
print('数据 行优先索引')
while not it.finished:
    print('{} {}'.format(it[0],it.index),end='| ')
    it.iternext()
#[[ 0  5 10]
# [15 20 25]
# [30 35 40]
# [45 50 55]]
#数据 行优先索引
#0 0| 5 1| 10 2| 15 3| 20 4| 25 5| 30 6| 35 7| 40 8| 45 9| 50 10| 55 11|

(,)索引
a = np.arange(0,60,5).reshape(4,3)
print(a)
it = np.nditer(a, flags=['multi_index'])
print('数据 (行,列)索引')
while not it.finished:
    print('{} {}'.format(it[0],it.multi_index),end='| ')
    it.iternext()
#[[ 0  5 10]
# [15 20 25]
# [30 35 40]
# [45 50 55]]
#数据 (行,列)索引
#0 (0, 0)| 5 (0, 1)| 10 (0, 2)| 15 (1, 0)| 20 (1, 1)| 25 (1, 2)| 30 (2, 0)| 35 (2, 1)| 40 (2, 2)| 45 (3, 0)| 50 (3, 1)| 55 (3, 2)|

order:控制遍历顺序。order=‘C’,C order,即是行序优先(默认)。order=‘F’,Fortran order,即是列序优先

import numpy as np 
a = np.arange(0,60,5) 
a = a.reshape(3,4)  
print ('原始数组是:')
print (a)
for x in np.nditer(a, order='C'):  
   print (x, end=", " )
print()
for x in np.nditer(a, order='F'):  
   print (x, end=", " )
原始数组是:
#[[ 0  5 10 15]
# [20 25 30 35]
# [40 45 50 55]]
#0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 
#0, 20, 40, 5, 25, 45, 10, 30, 50, 15, 35, 55,

广播迭代
如果两个数组是可广播的,nditer可以同时迭代两个数组

a = np.arange(0,60,5).reshape(3,4)
print(a)
b = np.array([1,2,3,4],dtype=int)
print(b)
for x,y in np.nditer([a,b]):
    print('{}: {}'.format(x,y),end=', ')
#[[ 0  5 10 15]
# [20 25 30 35]
# [40 45 50 55]]
#[1 2 3 4]
#0: 1, 5: 2, 10: 3, 15: 4, 20: 1, 25: 2, 30: 3, 35: 4, 40: 1, 45: 2, 50: 3, 55: 4, 

选取行

import numpy as np
a = np.array([[1,2],[3,4]])
for one in a:
    print(one)
#[1 2]
#[3 4]

flat属性

如果需要操作数组中的每一个元素,可以用flat属性,该属性是一个数组元素迭代器

import numpy as np
a = np.arange(0,12).reshape(4,3)
for element in a.flat:
	print(element,end=', ')
#0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,

flatten(order=‘C’)函数

flatten函数返回一份数组拷贝,默认按行。

import numpy as np
a = np.arange(9).reshape(3,3)
print(a.flatten())
#[0 1 2 3 4 5 6 7 8]
print(a.flatten(order='F'))
[0 3 6 1 4 7 2 5 8]

引用

https://www.runoob.com
np.nditer():numpy迭代器
官方文档

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值