import numpy as np a = np.array([[1,2,3,4],[9,0,2,3],[1,2,3,19]]) print("原数组",a) print("a数组的ID:",id(a)) b = a print("数组b的id:",id(b)) b.shape = 4,3; print("b数组形状的更改也会反映到a数组上:") print(a)输出结果:
原数组: [[1 2 3 4] [9 0 2 3] [1 2 3 19]] 数组a的ID:139663602288640 b数组的ID:139663602288640 b数组形状的更改也会反映给a数组上: [[ 1 2 3] [4 9 0] [2 3 1] [2 3 19]]
import numpy as np a = np.array([[1,2,3,4],[9,0,2,3],[1,2,3,19]]) print("原数组",a) print("数组a的ID:",id(a)) b = a.view() print("数组b的ID:",id(b)) #打印b数组 print(b) #改变b数组形状 b.shape = 4,3 print("原数组a",a) print("新数组b",b)输出结果:
原数组: [[ 1 2 3 4] [ 9 0 2 3] [ 1 2 3 19]] a数组id: 140280414447456 b数组id: 140280287000656 打印b数组 [[ 1 2 3 4] [ 9 0 2 3] [ 1 2 3 19]] 对数组b所做的更改不会反映到a数组上: 原a数组 [[ 1 2 3 4] [ 9 0 2 3] [ 1 2 3 19]] 新数组b [[ 1 2 3] [ 4 9 0] [ 2 3 1] [ 2 3 19]]
import numpy as np arr = np.arange(10) print ('数组arr:') print (arr) #创建切片修改原数组arr a=arr[3:] b=arr[3:] a[1]=123 b[2]=234 print(arr)输出结果:
arr数组: [ 0 1 2 3 4 5 6 7 8 9] 切片修改arr原数组: [ 0 1 2 3 123 234 6 7 8 9]
import numpy as np a = np.array([[1,2,3,4],[9,0,2,3],[1,2,3,19]]) print("原数组",a) print("a数组ID:",id(a)) b = a.copy() print("b数组ID:",id(b)) print("打印经过copy方法的b数组:") print(b) b.shape=4,3 print("原数组",a) print("经过copy方法的b数组",b)输出结果:
原始数组: [[ 1 2 3 4] [ 9 0 2 3] [ 1 2 3 19]] a数组ID: 139895697586176 b数组ID: 139895570139296 打印经过copy方法的b数组 [[ 1 2 3 4] [ 9 0 2 3] [ 1 2 3 19]] 对b数组的改变不会影响到a数组: 原数组 [[ 1 2 3 4] [ 9 0 2 3] [ 1 2 3 19]] 经过Copy方法的b数组 [[ 1 2 3] [ 4 9 0] [ 2 3 1] [ 2 3 19]]
本文链接:http://task.lmcjl.com/news/13681.html