Python data copying
#-*-Config = UTF-8-*-# copy data a = [, "a", "C"]; B =; # The address space of a and B is the same. append ("B"); B. append ("f"); print (B, a); # [1, 2, 3, 4, 5, 6, 'A', 'C', 'B ', 'F'] [1, 2, 3, 4, 5, 6, 'A', 'C', 'B ', 'F'] When a is changed, B also changes B a and also changes print (id (B), id ()); #11075528 the same address space ################################## ############ import copy; # copying is the application of data in the memory # This is the shortest copy list = [1, 2, 3, 4, ["a", "B", "c"]; list_1 = copy. copy (list); print (list, list_1) print (id (list), id (list_1); #11367752 10970440 different address spaces have different lists. append ("5"); print (list, list_1); # [1, 2, 3, 4, ['A', 'B', 'C'], '5'] [1, 2, 3, 4, ['A', 'B ', 'C'] list changed, but list_1 did not change print (id (list [0]), id (list_1 [0]); #1394274096 1394274096 the address space of their respective elements is still the same list [4]. append ("d"); print (list, list_1); # [1, 2, 3, 4, ['A', 'B', 'C ', 'D'], '5'] [1, 2, 3, 4, ['A', 'B', 'C ', 'D'] changed # Deep copy list_2 = [1, 2, 3, 4, ["a", "B", "c"]; list_3 = copy. deepcopy (list_2); print (id (list_2), id (list_3); #17402824 17402248 different print addresses (id (list_2 [4]), id (list_3 [4]); #11241032 different addresses of list_2 [4]. append ("d"); print (list_2, list_3); # [1, 2, 3, 4, ['A', 'B', 'C ', 'D'] [1, 2, 3, 4, ['A', 'B', 'C'] list_3 not changed