当前位置:网站首页>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]]
边栏推荐
- 第六届智能家居亚洲峰会暨精品展(Smart Home Asia 2022)将于10月在沪召开
- About ASM disk space full, clean up ASM disk
- How to choose an account opening broker? Is it safe to open an account online?
- Almost Union-Find(带权并查集)
- 【转载】STM32 GPIO类型
- WasmEdge 0.10.0 发布!全新的插件扩展机制、Socket API 增强、LLVM 14 支持
- Solve NPM err! Unexpected end of JSON input while parsing near
- 广州:金融新活水 文企新机遇
- Understanding of CUDA, cudnn and tensorrt
- 神殿
猜你喜欢

电子元器件销售ERP管理系统哪个比较好?

Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.

Webrtc advantages and module splitting

The maximum number of Rac open file descriptors, and the processing of hard check failure

WasmEdge 0.10.0 发布!全新的插件扩展机制、Socket API 增强、LLVM 14 支持

广州:金融新活水 文企新机遇

叠加阶梯图和线图及合并线图和针状图

TCP那点事

罗氏线圈可以测量的大电流和频率范围
![[untitled]](/img/bb/213f213c695795daecb81a4cf2adcd.jpg)
[untitled]
随机推荐
AI chief architect 8-aica-gao Xiang, in-depth understanding and practice of propeller 2.0
How do people over 40 allocate annuity insurance? Which product is more suitable?
Anniversary party
Priority of JS operator
第六届智能家居亚洲峰会暨精品展(Smart Home Asia 2022)将于10月在沪召开
Comment supprimer le crosstalk SiC MOSFET?
Set<String>
Why are function templates not partial specialization?
[introduction to SQL in 10 days] day5+6 consolidated table
[learning notes] simulation
【无标题】
Basic operation of PMP from applying for the exam to obtaining the certificate, a must see for understanding PMP
找合适的PMP机构只需2步搞定,一查二问
Modifying the SSH default port when installing Oracle RAC makes CRS unable to install
NPM clean cache
抖音服务器带宽有多大,才能供上亿人同时刷?
Cloudcompare & PCL point cloud SVD decomposition
PMP从报考到拿证基本操作,了解PMP必看篇
Where is CentOS mysql5.5 configuration file
如何抑制SiC MOSFET Crosstalk(串擾)?