当前位置:网站首页>Copy & Deepcopy
Copy & Deepcopy
2022-06-28 08:39:00 【王吉吉丫】
- copy()用来复制列表或字典里的值,并不是复制引用。以下代码中,a和a_copy是两个不同的列表,两者互不影响。
- deepcopy()可以复制列表里包含的子列表。以下代码中可以看出,copy()只拷贝了单层列表,而deepcopy()拷贝了嵌套列表。
import copy a = [1,2,3] b = [1, 2, [3, 4]] a_copy = a.copy() a_deepcopy = copy.deepcopy(a) b_copy = b.copy() b_deepcopy = copy.deepcopy(b) print(a == a_copy, a==a_deepcopy, a_copy==a_deepcopy) print(a is a_copy, a is a_deepcopy, a_copy is a_deepcopy) # Output: True True True # False False False print("a:",a, "a_copy:",a_copy, "a_deepcopy:",a_deepcopy) print("b:",b, "b_copy:",b_copy, "b_deepcopy:",b_deepcopy) # Output: a: [1, 2, 3] # a_copy: [1, 2, 3] # a_deepcopy: [1, 2, 3] # b: [1, 2, [3, 4]] # b_copy: [1, 2, [3, 4]] # b_deepcopy: [1, 2, [3, 4]] a[0] = 4 a.append(5) b[2][0] = "c" b.append(3) b.append([7,8]) print("a:",a, "a_copy:",a_copy, "a_deepcopy:",a_deepcopy) print("b:",b, "b_copy:",b_copy, "b_deepcopy:",b_deepcopy) # Output:a: [4, 2, 3, 5] # a_copy: [1, 2, 3] # a_deepcopy: [1, 2, 3] # b: [1, 2, ['c', 4], 3, [7, 8]] # b_copy: [1, 2, ['c', 4]] # b_deepcopy: [1, 2, [3, 4]]
边栏推荐
- 爱分析发布《2022爱分析 · IT运维厂商全景报告》 安超云强势入选!
- Tree
- Superimposed ladder diagram and line diagram and merged line diagram and needle diagram
- 抖音服務器帶寬有多大,才能供上億人同時刷?
- Trailing Zeroes (II)
- [untitled]
- Build the first neural network with pytoch and optimize it
- [learning notes] simulation
- Set<String>
- On the solution of insufficient swap partition
猜你喜欢
随机推荐
The Falling Leaves
Almost union find (weighted union search)
About ASM disk space full, clean up ASM disk
Which is a better ERP management system for electronic component sales?
Trailing Zeroes (II)
Solve NPM err! Unexpected end of JSON input while parsing near
Super Jumping! Jumping! Jumping!
[learning notes] search
11grac turn off archive log
CloudCompare&PCL 点云裁剪(基于封闭曲面或多边形)
中金财富开户安全吗?怎么收费?
Login common test case
AVFrame内存管理api
JS rounding tips
Selenium+chromedriver cannot open Google browser page
Kali Notes(1)
DELL R730服务器开机报错:[XXX] usb 1-1-port4: disabled by hub (EMI?), re-enabling...
神殿
CloudCompare&PCL 点云SVD分解
Sword finger offer 30 Stack containing min function








