List depth copy

# Prefix: 
# 1. For assignment operations, the pointers are all to the same memory address, and they all change.
# l1 = [1, 2, 3]
# l2 = l1
# l3 = l2
# l3.append(666) #Add an element to l3, and both l1 and l2 are added
# print(id(l1),id(l2 ),id(l3)) #The three memory addresses are the same
# print(l1, l2, l3)

# Result: 1414378562376 1414378562376 1414378562376
# [1, 2, 3, 666] [1, 2, 3, 666] [1 , 2, 3, 666]

# Add # to the image in Word

Shallow copy: For shallow copy, the first layer creates a new memory address, and starting from the second layer
# points to the same memory Addresses, so for the second layer and beyond, remain consistent.
# l1=[11,22,33]
# l2=l1.copy()
# print(l1,l2)
# print(id(l1),id(l2))
# result: [11, 22, 33] [11 , 22, 33]
# 2297283420872 2297283421000

# l1=[11,22,33]
# l2=l1.copy()
# l1.append(666)
# print(l1,l2)
# print(id(l1),id(l2))
# Result: [11, 22, 33, 666] [11, 22, 33]
# 2091097989832 2091097989960


# l1 = [11, 22, [55, 66], [ 11, 22]]
# l2 = l1.copy()
# l1[2].append('Xiaomi') #When adding elements of the second layer and above to one of the lists,
# print(l1,l2) Another list is also added automatically, because it is the same memory address
# print(id(l1),id(l2)) #The two memory addresses are different (one layer)
# print(id(l1[2]), id(l2[2])) #The memory addresses of the two are the same (layer 2 or above)
# Result: [11, 22, [55, 66, 'Xiaomi'], [11, 22]] [11 , 22, [55, 66, 'Xiaomi'], [11, 22]]
# 2235139611016 2235139610312
# 2235138777800 2235138777800
# Add pictures to find in QQ

# Deep copy deepcopy() Completely independent (each layer is independent, different memory address)

# One layer is a different memory address
# from copy import deepcopy #Call deep copy
# l1 = [11, 22, ['Xiaofennen', 66], [11, 'Wang Dachui']]
# l2 =deepcopy(l1)
# l2.append('Teacher Cang')
# print(l1,id( l1))
# print(l2,id(l2))

# Or:
# import copy #Call deep copy
# l1 = [11, 22, ['Xiaofennen', 66], [11, 'King's hammer']]
# l2 =copy.deepcopy(l1) #Pay attention to
the spelling# l2.append('Teacher Cang')
# print(l1,id(l1))
# print(l2,id(l2))
# Result: [11, 22, [ 'Xiaofennen', 66], [11, 'Wang Dachui']] 2121711904584
# [11, 22, ['Xiaofennen', 66], [11, 'Wang Dahui'], 'Teacher Cang'] 2121711904520

# 2 Layers and above are also different memory addresses
# import copy #Call deep copy
# l1 = [11, 22, ['Xiaofennen', 66], [11, 'Wang Dachui']]
# l2 =copy.deepcopy(l1 ) #Pay attention to the spelling
# l1[-1].append('Black Whirlwind')
# print(l1[-1],id(l1[-1]))
# print(l2[-1],id(l2[-1]))
# Result: [11, 'Sledgehammer', 'Black Whirlwind'] 2212367786568
# [11, 'Sledgehammer'] 2212367787592



# Supplement:
# s1 = 'alex'
# s2 = 'alex'
# Due to the concept of small data pools, s1, s2, is the same memory address


# l1 = [1, 2, 3]
# l2 = [1, 2, 3]
# These two are different memory addresses


# For slices, this is a shallow copy.
l1 = [1, 2, 3, 4, 5, 6, [11,22]]
l2 = l1[:]
# l1.append(666)
# print(l1, l2)
# print(id(l1),id (l2))

# l1 = [1, 2, 3, 4, 5, 6, [11,22]]
# l2 = l1[:]
# l1[-1].append(666)
# print(l1, l2 )
# print(id(l1[-1]), id(l2[-1]))
# Result: [1, 2, 3, 4, 5, 6, [11, 22, 666]] [1, 2 , 3, 4, 5, 6, [11, 22, 666]]
# 2880614507208 2880614507208

Guess you like

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