Python basic tutorial: detailed usage of deep and shallow copy

1. First look at the assignment operation

l1 = [1,2,3,['barry','alex']]
l2 = l1

l1[0] = 111
print(l1)  # [111, 2, 3, ['barry', 'alex']]
print(l2)  # [111, 2, 3, ['barry', 'alex']]

l1[3][0] = 'wusir'
print(l1)  # [111, 2, 3, ['wusir', 'alex']]
print(l2)  # [111, 2, 3, ['wusir', 'alex']]

For assignment operations, l1 and l2 point to the same memory address, so they are exactly the same.

2. Shallow copy

'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#同一代码块下:
l1 = [1, '太白', True, (1,2,3), [22, 33]]
l2 = l1.copy()
print(id(l1), id(l2))  # 2713214468360 2713214524680
print(id(l1[-2]), id(l2[-2]))  # 2547618888008 2547618888008
print(id(l1[-1]),id(l2[-1]))  # 2547620322952 2547620322952

# 不同代码块下:
>>> l1 = [1, '太白', True, (1, 2, 3), [22, 33]]
>>> l2 = l1.copy()
>>> print(id(l1), id(l2))
1477183162120 1477183162696
>>> print(id(l1[-2]), id(l2[-2]))
1477181814032 1477181814032
>>> print(id(l1[-1]), id(l2[-1]))
1477183162504 1477183162504

For shallow copy, it just recreates a space in memory to store a new list, but the elements in the new list are shared with the elements in the original list.

3. Deep copy deepcopy

# 同一代码块下
import copy
l1 = [1, 'alex', True, (1,2,3), [22, 33]]
l2 = copy.deepcopy(l1)
print(id(l1), id(l2))  # 2788324482440 2788324483016
print(id(l1[0]),id(l2[0]))  # 1470562768 1470562768
print(id(l1[-1]),id(l2[-1]))  # 2788324482632 2788324482696
print(id(l1[-2]),id(l2[-2]))  # 2788323047752 2788323047752

# 不同代码块下
>>> import copy
>>> l1 = [1, '太白', True, (1, 2, 3), [22, 33]]
>>> l2 = copy.deepcopy(l1)
>>> print(id(l1), id(l2))
1477183162824 1477183162632
>>> print(id(0), id(0))
1470562736 1470562736
>>> print(id(-2), id(-2))
1470562672 1470562672
>>> print(id(l1[-1]), id(l2[-1]))
1477183162120 1477183162312

For deep copy, the list is recreated in memory, the mutable data types in the list are recreated, and the immutable data types in the list are public.

1. Numbers and Strings

For numbers and strings, assignment, shallow copy, and deep copy are meaningless because they always point to the same memory address.

'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import copy
# ######### 数字、字符串 #########
n1 = 123
# n1 = "i am alex age 10"
print(id(n1))
# ## 赋值 ##
n2 = n1
print(id(n2))
# ## 浅拷贝 ##
n2 = copy.copy(n1)
print(id(n2))
  
# ## 深拷贝 ##
n3 = copy.deepcopy(n1)
print(id(n3))

insert image description here

2. Other basic data types

For dictionaries, tuples, and lists, the memory address changes are different when performing assignment, shallow copy, and deep copy.

1. Assignment

Assignment just creates a variable that points to the original memory address, such as:

n1 = {
    
    "k1": "wu", "k2": 123, "k3": ["alex", 456]}
  
n2 = n1

insert image description here2. Shallow copy

Shallow copy, only the first layer of data is created additionally in memory

import copy
  
n1 = {
    
    "k1": "wu", "k2": 123, "k3": ["alex", 456]}
  
n3 = copy.copy(n1)

insert image description here
3. Deep copy

Deep copy, recreate all data in memory (excluding the last layer, ie: python internal optimization of strings and numbers)

'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import copy
  
n1 = {
    
    "k1": "wu", "k2": 123, "k3": ["alex", 456]}
  
n4 = copy.deepcopy(n1)

insert image description here

Assignment operation:

l1 = [1,2,3]
l2 = l1
l2.append(666)
print(l1,l2)        #[1, 2, 3, 666] [1, 2, 3, 666]

For assignment operations, the pointers to the same memory address are always unchanged.

Shallow copy:

'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
l1 = [11,22,33]
l2 = l1.copy()
l1.append(666)
print(l1,id(l1))        #[11, 22, 33, 666] 31203144
print(l2,id(l2))        #[11, 22, 33] 31203272
l1 = [11, 22, ['barry', [55, 66]], [11, 22]]
l2 = l1.copy( )
l1[2].append('alex')
#print(l1,id(l1))          #11, 22, ['barry', [55, 66], 'alex'], [11, 22]] 41723272
#print(l2,id(l2))   #  [11, 22, ['barry', [55, 66], 'alex'], [11, 22]] 41723336
print(l1, id(l1[-1]))
print(12, id (l2[-1]))

For shallow copy, the first layer creates a new memory address, and starting from the second layer, it points to the same memory address, so for the second layer and deeper layers, keep consistency .

Deep copy: completely independent (copy its data completely and put it in a separate memory, completely copy, data is not shared)

import copy
l1 = [11,22, ['barry']]
l2 = copy.deepcopy(l1)
l1[2].append('alex')
print(l1,id(l1[-1]))   # [11,22, ['barry' , 'alex' ]] 42282312
print(l2, id(l2[-1]))  # [11,22, ['barry'] ] 42332680

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/120765749