deep copy and shallow copy

#deep copy

#shallow copy
#(1) Equal sign
# list1 = [11,22,33,['a','b']]
# list2 = list1
# list1.append(44)
# print(list1,list2,id(list1),id(list2))
# [11, 22, 33, ['a', 'b'], 44] [11, 22, 33, ['a', 'b'], 44] 43425416 43425416

#(2) Shallow copy copy
# import copy
# c = [11,22,33,['a','b']]
# d = copy.copy(c)
# c.append(44)
# c[3].append('c')
# print(c,d,id(c),id(d))
# #[11, 22, 33, ['a', 'b', 'c']] [11, 22, 33, ['a', 'b']] 43803848 43800456
# print(id(c[3]),id(d[3]))
# #43738248 43738248

# #deep copy deepcopy
import copy
c = [11,22,33,['a','b']]
d = copy.deepcopy(c)#
c.append(44)
c[3].append('c')
print(c,d,id(c),id(d))
#[11, 22, 33, ['a', 'b', 'c'], 44] [11, 22, 33, ['a', 'b']] 43931272 43933640
print(id(c[3]),id(d[3]))
#43929800 43931464
'''
Equal sign: belongs to shallow copy, does not change the id address, so changing one variable will also change another variable
Shallow copy: The id number of the first layer of data will change, but the id number in the singular data will not change, so if one variable changes, the other variable will not change.
But when the deeper data of the first variable changes, the data corresponding to another variable will change, right?
Deep copy deepcopy: will change the id number, including the id number of the element inside the element, so one variable changes, the other variable does not change (all data)
'''

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324647453&siteId=291194637