python浅复制与深复制

In python, the assignment operator does not copy the objects, instead, they create bindings between an object and the target. The object, if a collection that is mutable or consists of mutable items uses the copy so that one can change one copy without changing the other.

在python中,赋值运算符不复制对象,而是在对象和目标之间创建绑定。 如果该对象是可变的或由可变项组成的集合,则使用该副本,以便一个副本可以更改一个副本而无需更改另一个副本。

The module copy provides generic shallow and deep copy operations,

模块复制提供通用的浅层复制和深层复制操作

浅拷贝 (Shallow Copy)

A shallow copy constructs a new compound object and then inserts references into it to the objects found in the original. This process is not recursive and hence doesn't create copies of the child object. In shallow copy, a reference of object is copied to another object, meaning any changes made to the copy of the object shall reflect on the original object too.

浅表副本将构造一个新的复合对象,然后将对原始对象中找到的对象的引用插入其中。 此过程不是递归的,因此不会创建子对象的副本。 在浅表复制中,将对象的引用复制到另一个对象,这意味着对对象副本所做的任何更改也应反映在原始对象上。

    copy.copy(x)  # returns shallow copy

Example:

例:

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import copy
>>> xs = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> ys = copy.copy(xs)
>>> print(xs)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> print(ys)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> xs.append(['new sublist']) # Modifying the copied list at a "superficial" level was no problem at all.
>>> print(xs)
[[1, 2, 3], [4, 5, 6], [7, 8, 9], ['test']]
>>> print(ys)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> xs[1][0] = 'X1' // changes both 'xs' and 'ys'
>>> print(xs)
[[1, 2, 3], ['X1', 5, 6], [7, 8, 9], ['test']]
>>> print(ys)
[[1, 2, 3], ['X1', 5, 6], [7, 8, 9]]

深拷贝 (Deep Copy)

A deep copy constructs a new compound object and then recursively inserts the copies into it the objects found in the original.

深层副本会构造一个新的复合对象,然后将原始对象中找到的对象递归地插入副本。

    copy.deepcopy(x) # returns a deep copy

Example:

例:

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import copy
>>> ys = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> zs = copy.deepcopy(ys)
>>> ys[1][1] = 'X'
>>> print(ys)
[[1, 2, 3], [4, 'X', 6], [7, 8, 9]]
>>> print(zs)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>>

重要提醒 (Important reminders)

  • Shallow copying of an object will not clone the child objects. Hence, the child copy is not fully independent of the parent.

    浅复制对象不会克隆子对象。 因此,子副本并不完全独立于父副本。

  • A deep copy of an object will recursively clone the child object and hence the child object is fully independent of the parent. Creating a deep copy is slower.

    对象的深层副本将递归克隆子对象,因此子对象完全独立于父对象。 创建深层副本比较慢。

  • An arbitrary object, including custom classes, can be copied using the copy module.

    可以使用复制模块复制包括定制类在内的任意对象。

翻译自: https://www.includehelp.com/python/shallow-copy-vs-deep-copy.aspx

python浅复制与深复制

python浅复制与深复制_Python中的浅复制与深复制相关推荐

  1. Python基础_第3章_Python中的循环结构

    Python基础_第3章_Python中的循环结构 文章目录 Python基础_第3章_Python中的循环结构 Python中的循环结构 一.回顾分支练习题 1.判断是否为一个合法三角形 2.求世界 ...

  2. Python基础_第5章_Python中的数据序列

    Python基础_第5章_Python中的数据序列 文章目录 Python基础_第5章_Python中的数据序列 Python中的数据序列 一.字典--Python中的==查询==神器 1.为什么需要 ...

  3. python 获取用户的一个输入值_Python中,用于获取用户输入的命令为:

    [多选题]以下关于机器学习说法正确的是? [判断题]Python内置函数sum____用来返回数值型序列中所有元素之和. [单选题]关于自定义函数的下列说法不正确的是: [判断题]Python内置函数 ...

  4. python选取元音开头的单词_Python中的Regex,用于查找遵循以下模式的单词:元音、辅音、元音、辅音...

    如果将辅音有向图映射成单个辅音,则最长的单词是解剖病理学的10*VC字符串. 如果您正确映射y,那么您将得到完整的字符串,如乙酰丙酮作为8*VC,下胚轴作为8*CV. 如果不需要字符串是完整的,那么在 ...

  5. python 解压文件 重名_python小试身手-文件重命名,文件复制和压缩(.gz) - 铁匠铺的小铁匠...

    1 #!/usr/local/bin/python 2 #coding=UTF-8 3 4 importos5 importcsv as csv6 importre7 importshutil8 im ...

  6. python映射类型是什么意思_Python中字典映射类型的学习教程

    字典是python语言中唯一的映射类型,用花括号{}表示,一个字典条目就是一个键值对,方法keys()返回字典的键列表,values()返回字典的值列表,items()返回字典的键值对列表.字典中的值 ...

  7. python的pandas库内的函数_python 中NumPy和Pandas工具包中的函数使用笔记(方便自己查找)...

    'first' 按值在原始数据中出现的顺序排名 C.DataFrame DataFrame是一个表格型的数据结构,它含有一组有序的列,每列可以是不同的值类型(数值.字符串.布尔值等).DataFram ...

  8. python里的shell是什么_python中的shell操作

    http://blog.csdn.net/meng_tianshi/article/details/6682317 首先介绍一个函数: os.system(command) 这个函数可以调用shell ...

  9. python计算数组元素的和_python中数组的运算

    Python中进行数组的运算需要调用NumPy包. 其官网是:http://www.numpy.org/​www.numpy.org NumPy是Python语言的一个扩充程序库.它支持高级大量的维度 ...

最新文章

  1. XXL-REGISTRY v1.0.2 发布,分布式服务注册中心
  2. Spring Boot实战pdf
  3. GAN模型-分析角度
  4. 1040 Longest Symmetric String (25 分)_15行代码AC
  5. hive入门之安装模式
  6. android 为什么fragment在调用hide方法后没有生效_Android 多 Fragment 切换优化
  7. [js] 写一个方法实现promise失败后自动重试
  8. fscanf的返回值未成功输入的元素个数 .xml
  9. 2014年计算机初级应用考试是,2014年国硕士研究生入学统一考试计算机基础试题...
  10. 样本量很少如何获得最佳的效果?最新小样本学习工具包来啦!
  11. arp协议的主要功能是_【思唯网络学院】ARP理论知识详解(一)
  12. 头文件不是可有可无的
  13. Python的开源人脸识别库:离线识别率高达99.38%
  14. python:查看函数方法的具体信息、参数等
  15. 最新MT6763参考设计芯片资料
  16. Maven打包自定义MANIFEST.MF键值对
  17. k210安装ch210驱动(看着一篇就足够了)
  18. C#程序运行报错Error while trying to retrieve text for error ORA-12154
  19. Java if判断,while判断,Do while判断,Switch判断
  20. javaweb JAVA JSP运动会管理系统JSP运动会成绩管理系统 JSP校运会报名信息管理系统

热门文章

  1. MyEclipse在搭建s2sh时 如何 uninstalled facet
  2. java中自动装箱的问题
  3. vim配置之spacevim
  4. xargs 命令教程
  5. 计算机不能进入桌面,电脑开机无法进入桌面,请高手解决。
  6. java sax xml文件解析_java解析xml文件-DOM/SAX
  7. Pytorch Anaconda 安装CPU版本
  8. Angular 第一章 开始
  9. web项目上之深入理解Java国际化
  10. Mac 设置 NDK