标签:python
浅拷贝只拷贝最外层,里层拷贝的是内存地址的引用
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‘}]
标签:python
原文地址:http://chomper.blog.51cto.com/7866214/1937171