当前位置:网站首页>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]]
边栏推荐
猜你喜欢

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

Kali installation configuration

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

The micro kernel zephyr is supported by many manufacturers!

罗氏线圈工作原理

与普通探头相比,差分探头有哪些优点

Set the icon for the title section of the page

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

JS rounding tips

Usage record of Xintang nuc980: self made development board (based on nuc980dk61yc)
随机推荐
nuxt3入门
第六届智能家居亚洲峰会暨精品展(Smart Home Asia 2022)将于10月在沪召开
【转载】STM32 GPIO类型
Cloudcompare & PCL point cloud SVD decomposition
Case tool
yaml json
Robot Rapping Results Report
Solution: selenium common. exceptions. WebDriverException: Message: ‘chromedriver‘ execu
Kubernetes notes and the latest k3s installation introduction
Tree
【Go ~ 0到1 】 第一天 6月24 变量,条件判断 循环语句
Solve NPM err! Unexpected end of JSON input while parsing near
NPM clean cache
Almost union find (weighted union search)
Understanding of CUDA, cudnn and tensorrt
[learning notes] shortest path + spanning tree
Preparation for Oracle 11g RAC deployment on centos7
为什么函数模板没有偏特化?
WasmEdge 0.10.0 发布!全新的插件扩展机制、Socket API 增强、LLVM 14 支持
Where is CentOS mysql5.5 configuration file