当前位置:网站首页>Depth copy
Depth copy
2022-06-12 02:20:00 【Shallow water carp】
shallow copy
shallow copy Is that the memory points differently , Elements id Still use copy Previous , Variable and immutable are the same
1、 shallow copy A list is two lists ,id inequality
s1=[1,2,3,[11,22,33]]
s2=s1.copy()
s1.append(44)
print(s1,id(s1))#[1, 2, 3, [11, 22, 33], 44] 2090904676608
print(s2,id(s2))#[1, 2, 3, [11, 22, 33]] 2090907589440
2、 shallow copy The elements are the same ,id identical
s1=[1,2,3,[11,22,33]]
s2=s1.copy()
s1[-1].append(44)
print(s1,id(s1[-1]))#[1, 2, 3, [11, 22, 33, 44]] 2523862516992
print(s2,id(s2[-1]))#[1, 2, 3, [11, 22, 33, 44]] 2523862516992
# Opened up a new memory space ( Slot position ), Yes s1 Of copy Is a new slot , But the content still uses the previous address , So the list id Different , Elements id identical , In the slot is the memory number , Point to the same list
3、 Only the memory direction is changed
s1=[1,2,3,[11,22,33]]
s2=s1.copy()
s1[1]=12
print(s1,id(s1[1]))
#[1, 12, 3, [11, 22, 33]] 1960098163344
print(s2,id(s2[1]))
#[1, 2, 3, [11, 22, 33]] 1960098163024
4、 Another way of writing
import copy
s1=[1,2,3,[11,22,33]]
s2=copy.copy(s1)# shallow copy
print(s1,id(s1))#[1, 2, 3, [11, 22, 33]] 2537390491456
print(s2,id(s2))#[1, 2, 3, [11, 22, 33]] 2537393081856
print(s1 is s2)#False
print(id(s1[-1]))#2537393078336
print(id(s2[-1]))#2537393078336
deep copy
deep copy There are two tables , Elements id Different , however python Made an optimization ,python Keep immutable data types in the same , Point to the original copy Previous , Variable re creation , Include tuples
1、copy A list is two lists ,id inequality , Elemental id It's not the same
import copy
s1=[1,2,3,[11,22,33]]
s2=copy.deepcopy(s1)# deep copy
# s1[-1].append(666)
print(s1,id(s1))#[1, 2, 3, [11, 22, 33]] 3071881397824
print(s2,id(s2))#[1, 2, 3, [11, 22, 33]] 3071883988544
print(id(s1[-1]))#2435381575744
print(id(s2[-1]))#2435381576832
2、 Elemental id Dissimilarity , Not shared
import copy
s1=[1,2,3,[11,22,33]]
s2=copy.deepcopy(s1)# deep copy
s1[-1].append(666)
print(s1,id(s1[-1]))
#[1, 2, 3, [11, 22, 33, 666]] 2755944955968
print(s2,id(s2[-1]))
#[1, 2, 3, [11, 22, 33]] 2755944956992
3、 Optimize :python To deep copy Made an optimization , Keep immutable data types in the same , Point to the original copy Previous , Variable re creation , Include tuples
immutable :str、bool、int、tuple
variable :list、dict、set
import copy
s1=[1,2,3,[11,22,33]]
s2=copy.deepcopy(s1)
print(s1,id(s1[1]))#[1, 2, 3, [11, 22, 33]] 1924667763024
print(s2,id(s2[1]))#[1, 2, 3, [11, 22, 33]] 1924667763024
print(id(s1[-1]))#1924675977024
print(id(s2[-1]))#1924675978176
边栏推荐
- 力扣解法汇总449-序列化和反序列化二叉搜索树
- 力扣解法汇总905-按奇偶排序数组
- 力扣解法汇总398-随机数索引
- "China Dongxin Cup" the fourth programming competition of Guangxi University (synchronous competition)
- Graphic data analysis | business cognition and data exploration
- Force deduction solution summary 449 serialization and deserialization binary search tree
- leetcodeSQL:612. Nearest distance on plane
- Navicat for MySQL 11 Linux cracking method
- Force deduction solution summary 497 random points in non overlapping rectangles
- 力扣解法汇总668-乘法表中第k小的数
猜你喜欢

ozzanimation-基於sse的動作系統

Ozzanmation action system based on SSE

入手Ticwatch2

Glfwpollevents() program crash

代理与反射(二)

Hypergraph tilted data is merged into root node and transferred to 3dfiles

2022最全面的Redis事务控制(带图讲解)

Acl2022 | DCSR: a sentence aware contrastive learning method for open domain paragraph retrieval

How to automatically color cells in Excel

Basedexclassloader
随机推荐
力扣解法汇总668-乘法表中第k小的数
Force deduction solution summary 942- increase / decrease string matching
力扣解法汇总398-随机数索引
Several common instructions for virsh to create / shut down / stop virtual machines
Explore performance optimization! Performance improvement from 2 months to 4 hours!
力扣解法汇总473-火柴拼正方形
Xiaopi can't start the MySQL database. Please find out!
How should programmers solve the problem of buying vegetables? Take you hand in hand to quickly order and grab vegetables by using the barrier free auxiliary function
Oracle 11g graphic download installation tutorial (step by step)
程序员应该如何解决买菜难问题?手把手带你利用无障碍辅助功能快速下单抢菜
UE4\UE5触摸屏touch事件:单指、双指
Force deduction solution summary 824 goat Latin
力扣解法汇总905-按奇偶排序数组
中国银屑病药物市场评估与投资方向研究报告(2022版)
Force deduction solution summary 1022- sum of binary numbers from root to leaf
Force deduction solution summary 933- number of recent requests
Graphical data analysis | data analysis tool map
Installing MySQL version 5.5 database for Linux (centos6)
力扣解法汇总417-太平洋大西洋水流问题
SwiftyJSON解析本地JSON文件