用等于号进行的复制不会复制真正的数据,而是让等号两边的对象指向同一块内存区域
#Simple assignments make no copy of array objects or of their data.
a = np.arange(12)
b = a
# a and b are two names for the same ndarray object
b is a
b.shape = 3,4
print(a.shape) #也是3,4 b改a也改,因为a=b
print(id(a))#2222139585808
print(id(b))#2222139585808
使用了view函数后,浅拷贝如下:
#The view method creates a new array object that looks at the same data.
c = a.view()
c is a
c.shape = 2,6
print('a=',a)
print('c=',c)
print(c[0,4])
#print a.shape
c[0,4] = 1234
#注意,深层的地方一旦改变,a和c都会发生改变
print('a=',a)
print('c=',c)
'''
a= [[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
c= [[ 0 1 2 3 1234 5]
[ 6 7 8 9 10 11]]
1234
#把C[0,4]这个元素改动后,a和c均发生了变化
a= [[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
c= [[ 0 1 2 3 1234 5]
[ 6 7 8 9 10 11]]
'''
copy可以深复制,想要完全复制一个array变量,就需要使用copy函数。
#The copy method makes a complete copy of the array and its data.
d = a.copy()
d is a
d[0,0] = 9999
print(d)#d和a没啥联系了
print(a)#
'''
[[9999 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
'''