Python攻关之深浅拷贝

孤者浪人 提交于 2020-02-18 20:32:10

浅拷贝 = 只拷贝第一层
深拷贝 = 克隆一份

 s = [1,'zhang','age']
 s2 = s.copy()
 print(s2)
 s2[0] = 2
 print(s2)
 print(s)

 s = [[1,2],'zhang','age']
 s3 = s.copy()
 print(s3)

 s3[1] = 'liu'
 print(s3)
 print(s)

 s3[0][1] = 3
 print(s3)
 print(s)
 import copy
 husband = ['zhang',123,[15000,9000]]

 wife = husband.copy()
 wife[0] = 'liu'
 wife[1] = 345

 qu = copy.copy()   #浅拷贝
 qu = copy.deepcopy(husband)   #深拷贝
 qu[0] = 'qu'
 qu[1] = 666

 qu[2][1] -= 1999


 husband[2][1] -= 3000

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