浅拷贝的时候,修改原来的对象,深拷贝的对象不会发生改变。


对象的赋值


对象的赋值实际上是对象之间的引用:当创建一个对象,然后将这个对象赋值给另外一个变量的时候,python并没有拷贝这个对象,而只是拷贝了这个对象的引用。

aList = ["kel","abc",123]
print(aList, id(aList))
bList = aListbList.append("add")print(aList, id(aList))
print(bList, id(bList))
(['kel', 'abc', 123], 139637569314688)
(['kel', 'abc', 123, 'add'], 139637569314688)
(['kel', 'abc', 123, 'add'], 139637569314688)

同样 numpy 下的数据结构与数据类型的转换(np.array vs. np.asarray)

np.array() 是深拷贝,np.asarray() 是浅拷贝

两者主要的区别在于,array(默认)复制一份对象,asarray不会执行这一动作。

def asarray(a, dtype=None, order=None):return array(a, dtype, copy=False, order=order)

示例一

import numpy as np  arr1=np.ones((3,3))
arr2=np.array(arr1)
arr3=np.asarray(arr1)
print(arr2 is arr1)
print(arr3 is arr1)
print('arr1:',arr1, id(arr1))
print('arr2:',arr2, id(arr2))
print('arr3:',arr3, id(arr3))  
False
True
('arr1:', array([[ 1.,  1.,  1.],[ 1.,  1.,  1.],[ 1.,  1.,  1.]]), 139637569303856)
('arr2:', array([[ 1.,  1.,  1.],[ 1.,  1.,  1.],[ 1.,  1.,  1.]]), 139637569303776)
('arr3:', array([[ 1.,  1.,  1.],[ 1.,  1.,  1.],[ 1.,  1.,  1.]]), 139637569303856)

示例二

import numpy as np  arr1=np.ones((3,3))
arr2=np.array(arr1)
arr3=np.asarray(arr1)
arr1[1]=2
print('arr1:',arr1, id(arr1))
print('arr2:',arr2, id(arr2))
print('arr3:',arr3, id(arr3))  
('arr1:', array([[ 1.,  1.,  1.],[ 2.,  2.,  2.],[ 1.,  1.,  1.]]), 139637569303296)
('arr2:', array([[ 1.,  1.,  1.],[ 1.,  1.,  1.],[ 1.,  1.,  1.]]), 139637569303376)
('arr3:', array([[ 1.,  1.,  1.],[ 2.,  2.,  2.],[ 1.,  1.,  1.]]), 139637569303296)

对象的复制


当你想修改一个对象,而且让原始的对象不受影响的时候,那么就需要使用到对象的复制,对象的复制可以通过三种方法实现:

a、 使用切片操作进行拷贝--slice operation
b、 使用工厂函数进行拷贝,list/dir/set--factoryfunction
c、 copy.copy()--use copymodule

在复制的时候,使用的是浅拷贝,复制了对象,但是对象中的元素,依然使用引用。

person = ["name",["savings",100.00]]
hubby = person[:] #切片操作
wifey = list(person) #使用工厂函数[id(x) for x in person,hubby,wifey]
print("The person is:", person, id(person))
print("The hubby is:", hubby, id(hubby))
print("The wifey is:", wifey, id(wifey))
('The person is:', ['name', ['savings', 100.0]], 139637569566984)
('The hubby is:', ['name', ['savings', 100.0]], 139637569544848)
('The wifey is:', ['name', ['savings', 100.0]], 139637569405656)
print("The person inside is:", [id(x) for x in person])
print("The hubby inside is:", [id(x) for x in hubby])
print("The wifey inside is:", [id(x) for x in wifey])
('The person inside is:', [139639860076144, 139637569544344])
('The hubby inside is:', [139639860076144, 139637569544344])
('The wifey inside is:', [139639860076144, 139637569544344])

hubby[0] = "kel"
wifey[0] = "jane"
hubby[1][1] = 50.0
print("The person is:", person, id(person))
print("The hubby is:", hubby, id(hubby))
print("The wifey is:", wifey, id(wifey))
('The person is:', ['name', ['savings', 50.0]], 139637570044992)
('The hubby is:', ['kel', ['savings', 50.0]], 139637569460344)
('The wifey is:', ['jane', ['savings', 50.0]], 139637569406160)
print("The person inside is:", [id(x) for x in person])
print("The hubby inside is:", [id(x) for x in hubby])
print("The wifey inside is:", [id(x) for x in wifey])
('The person inside is:', [139639860076144, 139637569810016])
('The hubby inside is:', [139637569356104, 139637569810016])
('The wifey inside is:', [139637569378272, 139637569810016])

在使用浅拷贝的时候,发现引用的id都是相同的,但是字符串的id却发生了变化,是因为在python中,字符串是不可变的,从而在每次进行修改的时候,都是新建一个对象,从而引用发生了变化。


copy模块


浅拷贝和深拷贝的操作都可以在copy模块中找到,其实copy模块中只有两个函数可用,copy()进行浅拷贝操作,而deepcopy()进行深拷贝操作


#1
import copy
aList = [1,"kel",[1,2,3]]
print("The aList is:", aList, id(aList))shadeList = copy.copy(aList)
print("The shadeList is:", shadeList, id(shadeList))deepList = copy.deepcopy(aList)
print("The deepList is:", deepList, id(deepList))aList[2].append("kel")print("The aList is:", aList, id(aList))print("The shadeList is:", shadeList, id(shadeList))print("The deepList is:", deepList, id(deepList))
('The aList is:', [1, 'kel', [1, 2, 3]], 139639722291712)
('The shadeList is:', [1, 'kel', [1, 2, 3]], 139639722170344)
('The deepList is:', [1, 'kel', [1, 2, 3]], 139637569586096)
('The aList is:', [1, 'kel', [1, 2, 3, 'kel']], 139639722291712)
('The shadeList is:', [1, 'kel', [1, 2, 3, 'kel']], 139639722170344)
('The deepList is:', [1, 'kel', [1, 2, 3]], 139637569586096)

#2
import copy
aList = [1,"kel",[1,2,3]]
print("The aList is:", aList, id(aList))shadeList = copy.copy(aList)
print("The shadeList is:", shadeList, id(shadeList))deepList = copy.deepcopy(aList)
print("The deepList is:", deepList, id(deepList))shadeList[2].append("kel")print("The aList is:", aList, id(aList))print("The shadeList is:", shadeList, id(shadeList))print("The deepList is:", deepList, id(deepList))
('The aList is:', [1, 'kel', [1, 2, 3]], 139637569846448)
('The shadeList is:', [1, 'kel', [1, 2, 3]], 139637569406520)
('The deepList is:', [1, 'kel', [1, 2, 3]], 139637569407240)
('The aList is:', [1, 'kel', [1, 2, 3, 'kel']], 139637569846448)
('The shadeList is:', [1, 'kel', [1, 2, 3, 'kel']], 139637569406520)
('The deepList is:', [1, 'kel', [1, 2, 3]], 139637569407240)

#3
import copy
aList = [1,"kel",[1,2,3]]
print("The aList is:", aList, id(aList))shadeList = copy.copy(aList)
print("The shadeList is:", shadeList, id(shadeList))deepList = copy.deepcopy(aList)
print("The deepList is:", deepList, id(deepList))deepList[2].append("kel")
print("The deepList is:", deepList, id(deepList))print("The aList is:", aList, id(aList))print("The shadeList is:", shadeList, id(shadeList))
('The aList is:', [1, 'kel', [1, 2, 3]], 139637569460776)
('The shadeList is:', [1, 'kel', [1, 2, 3]], 139637569461496)
('The deepList is:', [1, 'kel', [1, 2, 3]], 139637569585592)
('The deepList is:', [1, 'kel', [1, 2, 3, 'kel']], 139637569585592)
('The aList is:', [1, 'kel', [1, 2, 3]], 139637569460776)
('The shadeList is:', [1, 'kel', [1, 2, 3]], 139637569461496)

参考文献


numpy 下的数据结构与数据类型的转换(np.array vs. np.asarray)

numpy中array和asarray的区别

python中的深拷贝与浅拷贝

python中的深拷贝与浅拷贝相关推荐

  1. python怎么避免浅拷贝_详谈Python中的深拷贝和浅拷贝

    在平时工作中,经常涉及到数据的传递,在数据传递使用过程中,可能会发生数据被修改的问题.为了防止数据被修改,就需要在传递一个副本,即使副本被修改,也不会影响原数据的使用.为了生成这个副本,就产生了拷贝. ...

  2. python中关于深拷贝和浅拷贝的详解

    python中关于深拷贝和浅拷贝的详解 概述 在python的语法中,有两种变量的拷贝方式 一种是深拷贝,一种是浅拷贝 我们先说深拷贝 语法 这里需要通过导入系统的copy模块中的deepcopy才可 ...

  3. python深浅拷贝的底层理解_理解python中的深拷贝与浅拷贝

    python中浅拷贝和深拷贝的区别如下: 1. copy.copy 等价于切片[:]  浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象.2个列表是不同的,但是子列表指向的仍然是同一个地址 2. co ...

  4. python中深拷贝和浅拷贝_**Python中的深拷贝和浅拷贝详解

    甚至连type其本身都是对象,type对象 Python中变量与C/C++/Java中不同,它是指对象的引用,Python是动态类型,程序运行时候,会根据对象的类型 来确认变量到底是什么类型. 单独赋 ...

  5. 关于python中的深拷贝和浅拷贝的总结

    可变对象和不可变对象 了解深拷贝和浅拷贝之前你得先知道什么时可变对象什么是不可变对象传送门 深拷贝和浅拷贝拷贝不可变对象时 import copy a = 1 b = copy.copy(a) c = ...

  6. python中的引用、浅拷贝和深拷贝

    在python中,有一句话:"一切皆为对象,一切皆为对象的引用",所以 只要记住这句话就很容易清楚python中的引用.浅拷贝和深拷贝了. 1. 引用 python中的引用是经常使 ...

  7. vb.net中递归退到最外层_面试题被问到再也不慌,深究JavaScript中的深拷贝与浅拷贝...

    " 点个关注,养成习惯,带你python爬虫的过程中学习前端 " JavaScript中的深拷贝和浅拷贝是前端面试中频繁被问到的一道题, 于是我也自己去查阅了一些资料, 然后动手敲 ...

  8. C语言中的深拷贝和浅拷贝

    http://www.cnblogs.com/zhanggaofeng/p/5421804.html C语言中的深拷贝和浅拷贝 //C语言中的深拷贝和浅拷贝 #define _CRT_SECURE_N ...

  9. js中的深拷贝和浅拷贝区别

    js中的深拷贝和浅拷贝与值传递和引用传递有着些许的关联,都是根据堆栈中数据的储存来传递数据. 下面主要说一下我对深拷贝和浅拷贝的理解: 简单举个例子来说明:当我们声明一个a变量并且赋值,并且让b等于a ...

最新文章

  1. 如何在Ubuntu Linux上开采以太坊?
  2. Javascript设置cookie和获取cookie
  3. 实例解说Linux命令行uniq (转)
  4. 我的Go+语言初体验——10秒真男人游戏
  5. 怎么删除mysql的压缩包_压缩包版mysql怎么卸载
  6. 【java基础知识】linux运行或停止jar包程序
  7. Ajax请求成功后页面跳转
  8. 无法打开匿名级安全令牌——CV明
  9. 随笔misc:sd卡速率测试用例
  10. RocketMQ实战2
  11. FairyGUI个人使用手册 只有重点(1)
  12. DSP RS232 RS485通信
  13. 【C语言--文件】(详细解读)
  14. 一键解决PPT的动画播放和动画排序问题!
  15. 触动精灵和按键精灵哪个好,如何用按键精灵ios触动精灵及脚本写自动答题脚本...
  16. 《诛仙》大量实景拍摄 程小东导演:演员状态自然
  17. 互联网行业竞争激烈 怎么做到不内卷不中年危机呢?
  18. 大厂常考机器学习面试题分享(下)
  19. bs4提取p标签内的值href
  20. [缓存]迅雷下载的原理——P2SP加速技术

热门文章

  1. 栈和排序(贪心+思维)
  2. 计算机2级学的是什么时候出来的,2019计算机二级考试科目有哪些 什么时候出成绩...
  3. 【计算机组成原理】浮点数运算及其流水线
  4. 实验7-3-6 字符串转换成十进制整数 (15分)
  5. win10专业版和企业版的区别_深度完美Win10_1809_LTSC.1158_64位企业版V2020.0415
  6. Java show两个整数加减_怎么样用java编写界面实现两个数的加法运算
  7. 机械硬盘 mysql调优_【MYSQL】使用RAID增加传统机器硬盘的性能
  8. js string转number_Node.js 和 C++ 之间的类型转换
  9. 已知两点坐标如何快速增加其他坐标_从0开发工程测绘大师小程序之坐标正算篇(十一)...
  10. Linux 系统配置Java Idea Tomcat 全过程