About Python's shades of Copy
1 ImportCopy2 3 #A shallow copy copies only the first layer, and the data in the back layer changes because the pointer follows the change.4Copyone = ["Walk", 28, [4300, 5000]]5CP1 =copyone.copy ()6Cp1[0] ='On_foot'7CP1[1] =' in'8Cp1[2][0] = 93009 Print("list Copyone:%s"%copyone)#after a shallow copy, 4300 of the original data has become 9300Ten Print("shallow copy effect:%s"%CP1) One A #deep copy is full copy -Copyone = ["Walk", 28, [4300, 5000]] -App =copy.deepcopy (Copyone) theCp2[0] ='On_foot' -CP2[1] =' in' -Cp2[2][0] = 9300 - Print("list Copyone:%s"%copyone)#no changes to the original list data after deep copy + Print("shallow copy effect:%s"%CP2)
The following results are performed:
List copyone: [' Walk ', 28, [9300, 5000]]
Shallow copy effect: [' on_foot ', ' 29 ', [9300, 5000]]
List copyone: [' Walk ', 28, [4300, 5000]]
Shallow copy effect: [' on_foot ', ' 29 ', [9300, 5000]]
A deep copy may not be as clear as that. But I know it already.
About Python's shades of Copy