补充_python深浅copy

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lm236236/article/details/96484055
name = ["liming","lilongfei","duanwenchao",["tangeng",11],"yuxiaobin"]
name2 = name.copy()   #浅copy,只copy第一层
print(name)
print(name2)

name[1] = "haha"   #当修改name的第一层时,name2不发生变化
name[3][1] = 22    #当修改name的第二层时,name2的第二层也发生相应的变化,原因是因为浅copy在copy第二层时,只是copy了他的内存地址
print(name)
print(name2)

#浅copy第二种
import copy
name = ["liming","lilongfei","duanwenchao",["tangeng",11],"yuxiaobin"]
name2 = copy.copy(name) 

#浅copy第三种
name2 = name[:]

#浅copy第四种
name2 = list(name)

#深copy,相当于做了复制,可以任意修改,不会影响其他副本
import copy
name = ["liming","lilongfei","duanwenchao",["tangeng",11],"yuxiaobin"]
name2 = copy.deepcopy(name) 

 

展开阅读全文

没有更多推荐了,返回首页