当前位置:网站首页>Shallow copy and deep copy
Shallow copy and deep copy
2022-07-18 22:52:00 【NeilNiu】
One 、 Shallow copy , Only the first level objects of variable types will be copied , Child objects are not copied , After the copy is successful, a new memory space will be opened to store the copy object
1、 Immutable type : Numbers 、 character string 、 Tuples
copy() Represents a shallow copy function
import copy
num1 = 1
num2 = copy.copy(num1)
# Check that the memory address has not changed , It indicates that the object is not copied , That is to say, there is no new memory space
print("num1:", id(num1), "num2:", id(num2))
my_tuple1 = (3, 5)
my_tuple2 = copy.copy(my_tuple1)
print("my_tuple1:", id(my_tuple1), "my_tuple2:", id(my_tuple2))
# Output
# num1: 8791254989888 num2: 8791254989888
# my_tuple1: 31363016 my_tuple2: 31363016summary : Shallow copy does not copy immutable types , In other words, it will not open up memory space .
A shallow copy of an immutable type is actually a copy of a reference , The two variables point to the same memory address , No new address opened .
2、 Variable type : list 、 Dictionaries 、 aggregate
# Variable type : list 、 Dictionaries 、 aggregate
my_list1 = [1, 3, [4, 6]]
my_list2 = copy.copy(my_list1)
print("my_list1:", id(my_list1), "my_list2:", id(my_list2))
my_list1.append(5)
print(my_list1, my_list2)
print("my_list1[2]", id(my_list1[2]), "my_list2[2]:", id(my_list2[2]))
# Output
# my_list1: 35415880 my_list2: 35416200
# [1, 3, [4, 6], 5] [1, 3, [4, 6]]
# my_list1[2] 35415816 my_list2[2]: 35415816For variable types , Shallow copy only copies the first layer , Child objects are not copied , So the memory address of the first layer has changed , But the memory address of the sub object does not change .
Two 、 Deep copy
deepcopy The function is a deep copy , As long as the object is found to have variable types , The object will be copied to each layer of the last variable type , For each layer of copied objects, new memory space will be opened up for storage
1、 Immutable type : Numbers 、 character string 、 Tuples
For numbers and strings , It's all immutable , So when using deep copy , Will not open up new memory space .
If there are variable type objects in tuples , After deep copy , Will open up a new memory address . If there are no mutable objects in tuples , After deep copy , Will not open up new memory address .
import copy
num1 = 1
num2 = copy.deepcopy(num1)
print("num1:", id(num1), "num2:", id(num2))
str1 = 'hello'
str2 = copy.deepcopy(str1)
print("str1:", id(str1), "str2:", id(str2))
my_tuple1 = (1, 2)
my_tuple2 = copy.deepcopy(my_tuple1)
print("my_tuple1:", id(my_tuple1), "my_tuple2:", id(my_tuple2))
my_tuple3 = (1, [1, 2])
my_tuple4 = copy.deepcopy(my_tuple3)
print("my_tuple3:", id(my_tuple3), "my_tuple4:", id(my_tuple4))
# Output
# num1: 8791250992192 num2: 8791250992192
# str1: 35017200 str2: 35017200
# my_tuple1: 34967496 my_tuple2: 34967496
# my_tuple3: 35475400 my_tuple4: 35350920
summary : If variable types are found in tuples , Tuples and sub element lists will be copied , After copying, a new memory space will be generated . But immutable types are not copied , Because immutable types do not allow data to be modified on the basis of the original memory space . So copying is meaningless . Because every copy , Memory address will change .
2、 Variable type : list 、 Dictionaries 、 aggregate
For deep copy, it will also be copied , If it is found that the child object is also a variable type, it will also be copied , After copying, new memory space will be opened to store the copied objects .
my_list1 = [1, [2, 3]]
my_list2 = copy.deepcopy(my_list1)
print("my_list1:", id(my_list1), "my_list2:", id(my_list2))
print("my_list1[0]:", id(my_list1[0]), "my_list2[0]:", id(my_list2[0]))
print("my_list1[1]:", id(my_list1[1]), "my_list2[1]:", id(my_list2[1]))
# Output
# my_list1: 31877064 my_list2: 31877384
# my_list1[0]: 8791250992192 my_list2[0]: 8791250992192
# my_list1[1]: 31877000 my_list2[1]: 31884040Whether it's a shallow copy or a deep copy , Are copies of variable types , Open up new memory space .
3、 ... and 、 The difference between a shallow copy and a deep copy
Shallow copy is the layer that copies the object at most
Deep copy may be multiple layers of copied objects
Four 、 summary
Shallow copy use copy.copy() function
Deep copy use copy.deepcopy() function
Whether it's a deep copy or a shallow copy of an object , As long as the copy is successful, it will open up a new memory space to store the copied object .
The difference between a shallow copy and a deep copy : Shallow copy is the layer that copies the object at most , Deep copy may copy multiple layers of objects
边栏推荐
- 1. Shell echo > and echo > >
- lun的概念
- 1. Create prism project
- Nature aging | activates FoxM1 gene, or doubles human life span
- 三维点云课程(一)——点云基础介绍
- Two stack implementation queue and two queue implementation stack (JS)
- C语言自定义类型:结构体,枚举,联合
- leetcode--两个数组交集2
- 练习动画最好的方式:封面过渡
- 2-conan binary package dependency management scheme
猜你喜欢

Encapsulate and obtain system user information, roles and authority control

In addition to Chang'an, these four domestic brands also use "Lexus face". Has Chinese design regressed?

Myql database suddenly disappeared, how to restore it?

Be diligent in chatting

Program analysis and Optimization - 11 multi branch analysis

sklearn线性回归拟合一次项函数

sklearn线性回归完成多次项函数和正弦函数拟合

Sklearn linear regression completes the fitting of multiple term function and sine function

德国rexroth比例阀4WRPEH6C5B40L-3X/M/24F1

416. Partition equal sum subset · knapsack problem · dynamic programming
随机推荐
Problem solving -- > online OJ (16)
[basic service] [database] MySQL master-slave replication deployment and configuration
Detailed explanation of bean's life cycle
npm安装教程
解题-->在线OJ(十六)
2022.7.15-----leetcode. five hundred and fifty-eight
416. Partition equal sum subset · knapsack problem · dynamic programming
Leetcode46. Full arrangement
2022年软考网络管理员备考指南
派克Parker比例阀D1FVE50BCVLB
Botu PLC Fuzzy PID control (quantitative factor and proportional factor)
Encapsulate and obtain system user information, roles and authority control
远程购买商品Solidity合约
npm 常用命令 使用命令删除 node_modules 包
关于产品 | 要怎么进行产品规划?
练习动画最好的方式:封面过渡
2022.7.14-----leetcode. seven hundred and forty-five
Peking University and Microsoft jointly proposed a super time series representation learning framework, which significantly improved the effect of multiple time series tasks
leetcode--49字母异位词分组
416.分割等和子集·背包问题·动态规划