brief introduction
When using a list or dictionary to pass parameters, you may encounter that the function changes the value of the list, but you don't want to remember the value in the original list. Therefore, python provides a copy module, which includes copy() and deepcopy(). As the name suggests, copy() refers to shallow copy and deepcopy() refers to deep copy.
copy.copy() detailed explanation
copy.copy() is mainly used to copy one-dimensional lists or one-dimensional tuples, such as ['A', 'B', 'C','d]. If there is another set of lists in the list, such as ['A', 'B','d ',' e ',' f ',' C], it cannot be copied or changed. Let's do A simple test.
import copy lis_A = ['A','B','C','D'] lis_B = ['A','B',['d','e','f'],'C'] # Use copy Copy() copy lis_A copy_A = copy.copy(lis_A) print('lis_A Value of', lis_A) print('copy_A Value of', copy_A) # Print out lis_A and copy_ ID value of a print('lis_A of ID value', id(lis_A)) print('copy_A of ID value', id(copy_A))
The output is:
lis_A Value of ['A', 'B', 'C', 'D'] copy_A Value of ['A', 'B', 'C', 'D'] lis_A of ID Value 1347357010368 copy_A of ID Value 1347357613888
It can be seen here. copy_ The value of a and LIS_ The value of a is the same, but their ID values are different, indicating copy_A points to a separate list. Then change copy_ The value of a will not affect LIS_ The list value of a can be tested:
As can be seen from the above, I changed copy_ The value of 'B' in a does not affect LIS_ The value in a.
What is the result of using copy() to copy nested lists?
lis_B = ['A', 'B', ['d', 'e', 'f'], 'C'] # Using copy_ LIS copy_B copy_B = copy.copy(lis_B) # Print LIS separately_ B and copy_ Value of B print('lis_B Value of', lis_B) print('copy_B Value of', copy_B) # Print out lis_B and copy_ ID value of B print('lis_B of ID value', id(lis_B)) print('copy_B of ID value', id(copy_B))
The output is:
lis_B Value of ['A', 'B', ['d', 'e', 'f'], 'C'] copy_B Value of ['A', 'B', ['d', 'e', 'f'], 'C'] lis_B of ID Value 2116281195712 copy_B of ID Value 2116321275968
Eh, it's also copied. What's going on? Don't worry. Keep looking. Let's change copy_ Try the value of B in B.
lis_B = ['A', 'B', ['d', 'e', 'f'], 'C'] # Using copy_B copy lis_B copy_B = copy.copy(lis_B) # Change copy_ Value of B in B copy_B[1] = 'change B' # Print LIS separately_ B and copy_ Value of B print('lis_B Value of', lis_B) print('copy_B Value of', copy_B) # Print out lis_B and copy_ ID value of B print('lis_B of ID value', id(lis_B)) print('copy_B of ID value', id(copy_B))
Output result:
lis_B Value of ['A', 'B', ['d', 'e', 'f'], 'C'] copy_B Value of ['A', 'change B', ['d', 'e', 'f'], 'C'] lis_B of ID Value 2258614705408 copy_B of ID Value 2258654720640
As can be seen from the above, copy_ The value of B in B has been changed. What's going on? Are you going to roll over?
Let's change copy again_ Try the whole list in B?
lis_B = ['A', 'B', ['d', 'e', 'f'], 'C'] # Using copy_B copy lis_B copy_B = copy.copy(lis_B) # # Change copy_ Value of B in B # copy_B[1] = 'change B' # Change LIS_ Try the nested list in B? copy_B[2] = ['1', '2', '3'] # Print LIS separately_ B and copy_ Value of B print('lis_B Value of', lis_B) print('copy_B Value of', copy_B) # Print out lis_B and copy_ ID value of B print('lis_B of ID value', id(lis_B)) print('copy_B of ID value', id(copy_B))
The output is:
lis_B Value of ['A', 'B', ['d', 'e', 'f'], 'C'] copy_B Value of ['A', 'B', ['1', '2', '3'], 'C'] lis_B of ID Value 1860576959872 copy_B of ID Value 1860618301312
copy_B's list has also changed. Changing copy_B what is the value of '2' in the nested list ['1', '2', '3']?
lis_B = ['A', 'B', ['d', 'e', 'f'], 'C'] # Using copy_B copy lis_B copy_B = copy.copy(lis_B) # # Change copy_ Value of B in B # copy_B[1] = 'change B' # Change LIS_ Try the nested list in B? copy_B[2] = ['1', '2', '3'] # Change LIS_ Try the values in the nested list in B? copy_B[2][1] = 'Change 2' # Print LIS separately_ B and copy_ Value of B print('lis_B Value of', lis_B) print('copy_B Value of', copy_B) # Print out lis_B and copy_ ID value of B print('lis_B of ID value', id(lis_B)) print('copy_B of ID value', id(copy_B))
Output result:
lis_B Value of ['A', 'B', ['d', 'e', 'f'], 'C'] copy_B Value of ['A', 'B', ['1', 'Change 2', '3'], 'C'] lis_B of ID Value 2763457256768 copy_B of ID Value 2763497140352
Ah, '2' has also changed, without affecting lis_B value, what's going on? Did you roll over?
We change the copy directly_ B's nested list value. Try it without changing the nested list first.
lis_B = ['A', 'B', ['d', 'e', 'f'], 'C'] # Using copy_B copy lis_B copy_B = copy.copy(lis_B) # # Change copy_ Value of B in B # copy_B[1] = 'change B' # # Change LIS_ Try the nested list in B? # copy_B[2] = ['1', '2', '3'] # # Change LIS_ Try the values in the nested list in B? # copy_B[2][1] = 'change 2' # Change copy directly_ B value, comment out the previous two steps, and copy_B = lis_B = ['A', 'B', ['d', 'e', 'f'], 'C'] copy_B[2][1] = 'Change 2' # Change 2 is to change E in ['a ',' B ','d', 'e', 'f'],'c '] # Print LIS separately_ B and copy_ Value of B print('lis_B Value of', lis_B) print('copy_B Value of', copy_B) # Print out lis_B and copy_ ID value of B print('lis_B of ID value', id(lis_B)) print('copy_B of ID value', id(copy_B))
Output result:
lis_B Value of ['A', 'B', ['d', 'Change 2', 'f'], 'C'] copy_B Value of ['A', 'B', ['d', 'Change 2', 'f'], 'C'] lis_B of ID Value 2342836779328 copy_B of ID Value 2342878850496
A magical scene happened. What changed was copy_ The value of e in B, but LIS_ The value of e in B has also changed.
do you understand? This is using copy Copy() has the disadvantage of copying nested lists. On the surface, it copies LIS_ B [but is lis_B completely copied? In this case, use deepcopy() to copy.
But why the previous situation Why can we change the nested list ['d ',' e ',' f '] to [' 1 ',' 2 ',' 3 '], and then change the' 2 'of [' 1 ',' 2 ',' 3] to 'change 2'.
(1) Why can ['A', 'B','d ',' e ',' f ',' C '] be changed into [' A ',' B ',' 1 ',' 2 ',' 3 ',' C ']?
Simply understand that in the overall change ['d ',' e ',' f '], it can be regarded as A whole, replaced by X and [' 1 ',' 2 ',' 3 '] replaced by Y. Therefore, the change at this time is equivalent to changing ['A', 'B', x, 'C'] to ['A', 'B', Y, 'C']. In fact, the change is still A one-dimensional list. copy.copy() can copy one-dimensional lists.
(2) Why copy_ When B = ['a', 'B','d ',' e ',' f ',' C '] changes to [' a ',' B ',' 1 ',' 2 ',' 3 '],' C '], and then changes to' 2 'in [' 1 ',' 2 ',' 3 '], LIS will not be affected_ B = [‘A’, ‘B’, [‘d’, ‘e’, ‘f’], ‘C’]?
The reason is that the first step is to copy_B = ['A', 'B','d ',' e ',' f ',' C '] becomes [' A ',' B ',' 1 ',' 2 ',' 3 '],' C '] at this time, A new list [' A ',' B ',' 1 ',' 2 ',' 3 '], and list LIS has been generated_ B = ['A', 'B','d ',' e ',' f '],' C '] are two completely different lists. Nature has no influence.
If there is A list lis_C = ['A', 'B','d ',' e ',' f ',' x ',' y ']], first change to [' A ',' B ',' 1 ',' 2 ',' 3 ',' x ',' y '], and then change to' x ', which will affect the source list
# Define a lis_C lis_C = ['A', 'B', ['d', 'e', 'f'], ['x', 'y']] # Copy a copy_C copy_C = copy.copy(lis_C) # copy_ C = ['a ',' B ','d', 'e', 'f'], ['x ',' y ']] becomes ['a', 'B', '1', '2', '3'], ['x ',' y ']] copy_C[2] = ['1', '2', '3'] # Change the value of 'x' in ['a ',' B ',' 1 ',' 2 ',' 3 '], ['x', 'y']] copy_C[3][0] = 'change x' # Print copy separately_ C and copy_ Value of C print('lis_C Value of', lis_C) print('copy_C Value of', copy_C) # Print out lis_C and copy_ ID value of C print('lis_C of ID value', id(lis_C)) print('copy_C of ID value', id(copy_C))
Output result:
lis_C Value of ['A', 'B', ['d', 'e', 'f'], ['change x', 'y']] copy_C Value of ['A', 'B', ['1', '2', '3'], ['change x', 'y']] lis_C of ID Value 2790729135616 copy_C of ID Value 2790729135424
As you can see, copy_ When ['d ',' e ',' f '] in C becomes [' 1 ',' 2 ',' 3 '], LIS is not affected_ C. When you change ['x', 'y'] to ['change X', 'y'], you will get the impression of lis_C
copy. Detailed explanation of deepcopy()
As mentioned above, when using copy Copy() copies the nested two-dimensional list ['A', 'B','d ',' e ',' f ',' x ',' y ']], and then changes the value in the nested list. If yes, it will affect the value of the source list, then use copy Does deepcopy () affect the source list?
import copy # Define a lis_D lis_D = ['A', 'B', ['d', 'e', 'f'], ['x', 'y']] # Use deepcopy() to copy a copy_C copy_D = copy.deepcopy(lis_D) # Change copy directly_ Value D in nested list ['d ',' e ',' f '] in D copy_D[2][0] = 'change d' # Print copy separately_ D and copy_ Value of D print('lis_D Value of', lis_D) print('copy_D Value of', copy_D) # Print out lis_D and copy_ ID value of D print('lis_D of ID value', id(lis_D)) print('copy_D of ID value', id(copy_D))
Output result:
lis_D Value of ['A', 'B', ['d', 'e', 'f'], ['x', 'y']] copy_D Value of ['A', 'B', ['change d', 'e', 'f'], ['x', 'y']] lis_D of ID Value 2335362856512 copy_D of ID Value 2335362856320
From the above results, it is obvious that the list lis is copied with deepcopy()_ After that, directly change the value d in the two-dimensional list, which will not affect the source list lis_D