首页 > 代码库 > 深浅拷贝
深浅拷贝
浅拷贝只拷贝最外层,里层拷贝的是内存地址的引用
In [1]: import copy In [41]: a1 Out[41]: [1, 2, 3, 4, {‘k1‘: ‘v1‘}] In [42]: a2 = copy.copy(a1) In [43]: a2[4][‘k1‘] = ‘v3‘ In [44]: a2 Out[44]: [1, 2, 3, 4, {‘k1‘: ‘v3‘}] In [45]: a1 Out[45]: [1, 2, 3, 4, {‘k1‘: ‘v3‘}]
深拷贝数据都拷贝了
In [34]: a1 Out[34]: [1, 2, 3, 4, {‘k1‘: ‘v1‘}] In [35]: a3 = copy.deepcopy(a1) In [36]: a3 Out[36]: [1, 2, 3, 4, {‘k1‘: ‘v1‘}] In [37]: a3[4] Out[37]: {‘k1‘: ‘v1‘} In [38]: a3[4][‘k1‘] = ‘v3‘ In [39]: a3 Out[39]: [1, 2, 3, 4, {‘k1‘: ‘v3‘}] In [40]: a1 Out[40]: [1, 2, 3, 4, {‘k1‘: ‘v1‘}]
深浅拷贝
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。