深浅拷贝_python

一、浅拷贝

拷贝第一层的东西,如其他列表修改他们共同的第二层(或更深),他管不了,只能跟着变。

用处:很少用,用不同账号关联共享:

import copy
husband= ['chen','123',[15000,9000]]
wife=copy.copy(husband)
wife[0]="wang"
wife[1]='234' #修改第一层,没有变化
print(husband) #['chen', '123', [15000, 9000]]
wife[2][1]=12000 #修改第二层,会有所变化
print(husband) #['chen', '123', [15000, 12000]]



二、深拷贝
完全复制,全部修改不影响
import copy
husband= ['chen','123',[15000,9000]]
wife=copy.deepcopy(husband)
wife[0]="wang"
wife[1]='234' #修改第一层,没有变化
print(husband) #['chen', '123', [15000, 9000]]
wife[2][1]=12000 #修改第二层,没有变化
print(husband) #['chen', '123', [15000, 9000]]

猜你喜欢

转载自www.cnblogs.com/chenxiaozan/p/12121612.html
今日推荐