Python数据分析基础与应用电子活页3-15遍历数组时控制能否修改数组元素值.docx
Python数据分析基础与应用
模块
PAGE2
PAGE21
电子活页3-15遍历数组时控制能否修改数组元素值
【技能训练3-26】以指定模式使用nditer迭代器结合for循环遍历NumPy的数组元素
【训练要求】
在JupyterNotebook开发环境中创建j3-26.ipynb,然后编写代码使用arange()函数创建一个3×4数组,并以指定模式(只读模式、读写模式、只写模式)使用nditer迭代器结合for循环遍历NumPy的数组元素。
【实施过程】
代码如下:
importnumpyasnp
array1=np.arange(0,60,5)
array2=array1.reshape(3,4)
print(原数组是:\n,array2)
foriteminnp.nditer(array2,op_flags=[read-write]):
item[...]=2*item
print(修改后的数组是:\n,array2)
输出结果:
原数组是:
[[051015]
[20253035]
[40455055]]
修改后的数组是:
[[0102030]
[40506070]
[8090100110]]