当前位置:网站首页>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]]
边栏推荐
- Not so Mobile
- Love analysis released the 2022 love analysis · it operation and maintenance manufacturer panorama report, and an Chao cloud was strongly selected!
- RAC enable archive log
- Resolution of Rac grid failure to start after server restart
- [learning notes] simulation
- 爱分析发布《2022爱分析 · IT运维厂商全景报告》 安超云强势入选!
- Guangzhou: new financial activities and new opportunities for enterprises
- Redis deployment under Linux & redis startup
- The 6th smart home Asia 2022 will be held in Shanghai in October
- AI chief architect 8-aica-gao Xiang, in-depth understanding and practice of propeller 2.0
猜你喜欢

VMware Workstation related issues

抖音服务器带宽有多大,才能供上亿人同时刷?

The 6th smart home Asia 2022 will be held in Shanghai in October

What is the bandwidth of the Tiktok server that can be used by hundreds of millions of people at the same time?

Installing mysql5.7 under Windows

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

Two tips for block level elements

用Pytorch搭建第一个神经网络且进行优化

Login common test case
![[untitled]](/img/bb/213f213c695795daecb81a4cf2adcd.jpg)
[untitled]
随机推荐
【大案例】学成在线网站
[learning notes] linear basis
【Go ~ 0到1 】 第二天 6月25 Switch语句,数组的声明与遍历
【力扣10天SQL入门】Day5+6 合并表
Selenium+chromedriver cannot open Google browser page
Build the first neural network with pytoch and optimize it
Lilda low code data large screen, leveling the threshold of data application development
How do individuals open accounts to speculate in stocks? Is online account opening safe?
Trailing Zeroes (II)
[learning notes] search
AWS builds a virtual infrastructure including servers and networks (2)
887. egg drop
Understanding of CUDA, cudnn and tensorrt
叠加阶梯图和线图及合并线图和针状图
Oracle RAC -- understanding of VIP
Anniversary party
Super Jumping! Jumping! Jumping!
【Go ~ 0到1 】 第一天 6月24 变量,条件判断 循环语句
Sword finger offer 03 Duplicate number in array
CloudCompare&PCL 点云裁剪(基于封闭曲面或多边形)