Today to tell you the depth of the copy, the depth copy needs to use the Copy module, here need to import the copy module
Import Copy
Today's blog structure, the first pair of strings and numbers of the two categories using assignment, shallow copy, deep copy
1, first look at the method of assignment
A1 = "abc" a2 = "123" B1 = A1B2 = A2print (ID (A1), ID (B1), sep= "/", end= "\ n") # 27299360/27299360print (ID (A2), id (B2), sep= "/", End= "\ n") # 30486656/30486656b1 = "Add" B2 = "789" Print (B1,A1) # Add Abcprint (B2,A2) # 789 123
2, in view of the method of shallow copy
A3 = copy.copy (a1) a4 = copy.copy (a2) print (ID (A3), ID (A1), sep= "/", end= "\ n") # 7179808/7179808print (ID (A4), id (A2), sep= " /", end=" \ n ") # 7876736/7876736a3 =" had "a4 =" 678 "Print (A3,A1) # had abcprint (A4,A2) # 678 123
3, the final look at the method of deep copy
A5 = copy.deepcopy (a1) A6 = copy.deepcopy (A2) print (ID (A5), id (A1), sep= "/", end= "\ n") # 26840608/26840608print (ID (A6), id (A2), sep= "/", end= "\ n") # 27537536/27537536a5 = "def" a6 = "456" Print (A5,A1) # def abcprint (A6,A2) # 456 123
Conclusion: for both strings and numbers, both the assignment, the shallow copy and the deep copy have no effect on the original variable.
In view of the assignment, shallow copy, deep copy of the effect on the list and the dictionary, in fact, the effect of the above on lists and dict is the same
First, we use list to give an example, first say the next no nested list
1, the first to assign value
L1 = ["A", "B", "C"]L2 = L1print (ID (L1), id (L2), sep= "/", end= "\ n") # 30744488/30744488l2.append ("D") print (l2,l1,sep= "/", End= "\ n") # [' A ', ' B ', ' C ', ' d '/[' A ', ' B ', ' C ', ' d ']print (ID (L1), id (L2), sep= "/", end= "\ n") # 28516264/28516264
2, in view of the shallow copy
L1 = ["A", "B", "C"]l2 = copy.copy (L1) print (ID (L1), id (L2), sep= "/", end= "\ n") # 30892240/30893000l2.append ("D") print (L1, L2) # [' A ', ' B ', ' C '] [' A ', ' B ', ' C ', ' d ']print (ID (L1), id (L2), sep= "/", end= "\ n") # 31088848/31089608
3, finally in view of the deep copy
L1 = ["A", "B", "C"]l2 = copy.deepcopy (L1) print (L1,L2) # [' A ', ' B ', ' C '] [' A ', ' B ', ' C ']print (ID (L1), id (L2), sep= "/", end= "\ n ") # 28450728/28467408l2.append (" D ") print (L1,L2) # [' A ', ' B ', ' C '] [' A ', ' B ', ' C ', ' d ']print (ID (L1), id (L2), sep="/", end=" \ n ") # 28712872/28729552
Conclusion: In the case of a list or dictionary without nesting, if the method of assignment is used, modifying one variable will affect the other, and for deep and shallow copies, one variable is not affected by the other.
And then we're looking at it in a nested dictionary.
1.
D1 = {"K1": "V1", "K2": "V2", "K3": ["a", "B", "C"]}D2 = D1print (ID (d1), ID (D2)) # 27455632 27455632d2["K1"] = "V1" Print (D2,D1) # {' K1 ': ' V1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']} {' K1 ': ' V1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']}print (ID (d1), ID (D2)) # 271 27952 27127952d2["K3"][0] = "a" Print (D1,D2) # {' K1 ': ' V1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']} {' K1 ': ' V1 ', ' K2 ': ' v2 ', ' K3 ' : [' A ', ' B ', ' C ']}print (ID (d1), ID (D2)) # 26931344 26931344
2, in view of the shallow copy
D1 = {"K1": "V1", "K2": "V2", "K3": ["a", "B", "C"]}d2 = copy.copy (d1) print (ID (d1), ID (D2)) # 27455680 27831392d2["K1"] = "V1" Print (D1,D2) # {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']} {' K1 ': ' V1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']}print (ID (D1), ID (D2)) # 7205056 7253088d2["K3"][0] = "a" Print (D1,D2) # {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']} {' K1 ': ' V1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']}print (ID (d1), ID (D2)) # 26931392 29797472
3, finally looking at the deep copy
D1 = {"K1": "V1", "K2": "V2", "K3": ["a", "B", "C"]}d2 = copy.deepcopy (d1) print (ID (d1), ID (D2)) # 27586704 30576880print (D1, D2) # {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']} {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']}d2[' k1 '] = ' V1 ' Print (d 1,D2) # {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']} {' K1 ': ' V1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']}print (ID (d1), ID (D2)) # 27062416 28348656d2["K3"][0] = "A" Print (ID (d1), ID (D2)) # 27193488 28152048print (d1,d2) # {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']} {' K1 ': ' V1 ', ' K2 ': ' v2 ', ' K3 ': [' A ', ' B ', ' C ']}
Conclusion: For the method of assignment, either modifying the value of the first layer or the value of the second layer will affect the other variable; for a shallow copy, modifying the value of the first layer has no effect on another variable, but modifying the value of the second layer will modify the original variable synchronously; for deep copies, Whether you modify the first layer or the second layer or even more layers, it has no effect on the original variable.
Python's shades of Copy