当前位置:网站首页>Copy & Deepcopy
Copy & Deepcopy
2022-06-28 08:45:00 【Wang jijiya】
- copy() Used to copy values from lists or dictionaries , It's not copying references . In the following code ,a and a_copy There are two different lists , They don't affect each other .
- deepcopy() You can copy the sub lists contained in the list . As you can see in the following code ,copy() Only a single level list is copied , and deepcopy() Nested list copied .
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]]
边栏推荐
猜你喜欢
![[learning notes] shortest path + spanning tree](/img/38/42b7e321389e3a47304fca5c37a2cf.png)
[learning notes] shortest path + spanning tree

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

Superimposed ladder diagram and line diagram and merged line diagram and needle diagram

Guangzhou: new financial activities and new opportunities for enterprises

Mysql8.0 forgot the root password

隐私计算FATE-----离线预测

爱分析发布《2022爱分析 · IT运维厂商全景报告》 安超云强势入选!

【大案例】学成在线网站

Discussion on safety management of centralized maintenance construction site of substation under the mode of operation and maintenance

Infinite penetration test
随机推荐
A - Bi-shoe and Phi-shoe
Integer partition
Common faults and solutions of Substation
RAC enable archive log
Tree
【无标题】
[learning notes] shortest path + spanning tree
Sword finger offer 30 Stack containing min function
Selenium reptile
The 6th smart home Asia 2022 will be held in Shanghai in October
[learning notes] simulation
TCP
AI chief architect 8-aica-gao Xiang, in-depth understanding and practice of propeller 2.0
利尔达低代码数据大屏,铲平数据应用开发门槛
Construire le premier réseau neuronal avec pytorch et optimiser
Lilda low code data large screen, leveling the threshold of data application development
如何抑制SiC MOSFET Crosstalk(串擾)?
Preparation for Oracle 11g RAC deployment on centos7
Build an integrated kubernetes in Fedora
Understanding of CUDA, cudnn and tensorrt