深浅拷贝
s = [23,'alex','ss','2s']
# s1 = [1,'alex','ss','s']
s2 = s.copy()
s2[0] = 1
print(s)
print(s2)
浅拷贝
s = [[1,2],'alex','alvin']
s3 = s.copy()
s3[0][1] = 3
print(s3)
print(s)
浅拷贝,只拷贝第一层 后面的共享
a = [[1, 2], 3, 4]
b2 = a
b = a.copy()
b2[1] = 'abc'
print(b2)
b[2] = '222'
print(a)
print(b)
应用银行关联账号
husband = ["xiaohu",123,[15000,9000]]
wife = husband.copy()
wife[0]="xiaopang"
wife[1] = "456"
print(wife)
husband[2][1] = 6000
print(wife)
深拷贝 克隆
import copy
xiaosan = copy.deepcopy(husband)
xiaosan[0] = "jinxin"
xiaosan[1] = 666
xiaosan[2][1] -= 1999
print(wife)
print(xiaosan)
print(husband)
网友评论