About the copy of the multidimensional list of Python, the behavior is difficult for me to understand, so I organized it with the experimental results.
In conclusion, copy ()
, list [:]
, and copy.copy ()
in the copy library are shallow copies, creating a new multidimensional list and then the original. Insert a reference to the list found in the list.
Copy.deepcopy ()
in the copy library is a deep copy, which creates a new multidimensional list and then inserts a copy of the list found in the original list.
copy()
I'm not sure if the "first layer" is better or the "first dimension" is better ... The ID is naturally different because the copy creates another multidimensional list. However, since the contents of the list are references, rewriting the first layer of the copy does not affect the original list, but rewriting the second and subsequent layers spreads to the original even though the ID is different.
copy()
lst = [[0]*3 for _ in range(3)]
lst1 = lst.copy()
lst1[1] = ['r'] * 3
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))
lst1[0][1] = 'r'
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))
output
lst1 = [[0, 0, 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 139730947400512
lst = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 139730946890944
lst1 = [[0, 'r', 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 139730947400512
lst = [[0, 'r', 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 139730946890944
List [:]
Same as copy ()
.
python:list[:]
lst = [[0]*3 for _ in range(3)]
lst1 = lst[:]
lst1[1] = ['r'] * 3
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))
lst1[0][1] = 'r'
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))
output
lst1 = [[0, 0, 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 140313735131456
lst = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 140313734622336
lst1 = [[0, 'r', 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 140313735131456
lst = [[0, 'r', 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 140313734622336
copy.copy ()
Same as copy ()
.
copy.copy()
import copy
lst = [[0]*3 for _ in range(3)]
lst1 = copy.copy(lst)
lst1[1] = ['r'] * 3
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))
lst1[0][1] = 'r'
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))
output
lst1 = [[0, 0, 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 139783910001664
lst = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 139783910057280
lst1 = [[0, 'r', 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 139783910001664
lst = [[0, 'r', 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 139783910057280
copy.deepcopy ()
Since copying creates another multidimensional list, the ID will be different and the contents of that list will also be copied. So to speak, a clone is made. The content is exactly the same, but different, so rewriting it will not affect the original.
copy.deepcopy()
import copy
lst = [[0]*3 for _ in range(3)]
lst1 = copy.deepcopy(lst)
lst1[1] = ['r'] * 3
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))
lst1[0][1] = 'r'
print('lst1 = ', lst1)
print('id(lst1) = %d' % id(lst1))
print('lst = ', lst)
print('id(lst) = %d' % id(lst))
output
lst1 = [[0, 0, 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 139629482679488
lst = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 139629482735040
lst1 = [[0, 'r', 0], ['r', 'r', 'r'], [0, 0, 0]]
id(lst1) = 139629482679488
lst = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
id(lst) = 139629482735040
Python --Duplicate list copy --- shallow copy and deep copy operations [Python] How to prevent the original list from being modified by assigning a list to the argument of a function you created
Recommended Posts