python-深浅拷贝

微笑、不失礼 提交于 2020-03-19 17:22:25

a=[1,2,3,4,5]
b=a
针对复杂数据类型(字符串和数字除外):a和b指向同一块内存空间,其中任一个发生改变,另外一个也会改变

浅拷贝只拷贝一层:
husband = ['xiaohua',123,[10000,6000]]
wife = husband.copy()
wife[0] = 'xiaopang'
wife[1] = 345
husband[2][1] -= 1500
print(wife)

深拷贝:克隆一份
import copy
husband = ['xiaohua',123,[10000,6000]]
wife = copy.copy(husband) #浅拷贝
xiaosan = copy.deepcopy(husband)
xiaosan[0] = 'jinxin'
xiaosan[1] = 666
xiaosan[2][1] -= 1000
husband[2][1] -= 1500
print(wife)
print(xiaosan)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!