####深浅拷贝#####
nums1 = [1,2,3,4,5]
nums2 = nums1 #不是拷贝,是一个指向,是赋值
nums3 = nums1.copy() #浅拷贝,拷贝同样内容,地址不一样,不是同一个对象,浅拷贝只拷贝第一层数据
nums2[0] = 11
print(nums1,nums2,nums3) #[11, 2, 3, 4, 5] [11, 2, 3, 4, 5] [1, 2, 3, 4, 5]
print(id(nums1),id(nums2),id(nums3))#4396862656 4396862656 4396862720
import copy
ww1 = ["hello","good",[11,22,33],"123"]
ww2 = ww1.copy()
print(ww1)#['hello', 'good', [11, 22, 33], '123']
print(ww2)#['hello', 'good', [11, 22, 33], '123']
ww2[0] = 22
print(ww1)#['hello', 'good', [11, 22, 33], '123']
print(ww2)#[22, 'good', [11, 22, 33], '123']
ww1[2][0] = 9999 #浅拷贝只拷贝第一层
print(ww1) #['hello', 'good', [9999, 22, 33], '123']
print(ww2) #[22, 'good', [9999, 22, 33], '123']
#深拷贝只能使用copy模块
#深拷贝 deepcopy
ww3 = copy.deepcopy(ww1)
ww1[2][1] = 1000000
print(ww1)#['hello', 'good', [9999, 1000000, 33], '123']
print(ww3)#['hello', 'good', [9999, 22, 33], '123']
网友评论