Python 深拷贝和浅拷贝详解

1、Python 深拷贝和浅拷贝概念理解

  • 浅拷贝,指的是重新分配一块内存创建一个新的对象,但里面的元素是原对象中各个子对象的引用

  • 深拷贝,是指重新分配一块内存,创建一个新的对象,并且将原对象中的元素,以递归的方式,通过创建新的子对象拷贝到新对象中。因此,新对象和原对象没有任何关联

2、浅拷贝

  • 使用数据类型本身的构造器
  • 对于可变的序列,还可以通过切片操作符 : 来完成浅拷贝
  • Python 还提供了对应的函数 copy.copy() 函数,适用于任何数据类型

2.1 使用数据类型本身的构造器

list1 = [1, 2, 3]
list2 = list(list1)
print(list2)
print("list1==list2 ?",list1==list2)
print("list1 is list2 ?",list1 is list2)

set1= set([1, 2, 3])
set2 = set(set1)
print(set2)
print("set1==set2 ?",set1==set2)
print("set1 is set2 ?",set1 is set2)

dict1 = {1:[1,'w'], 2:0, 3:98}
dict2 = dict(dict1)
print(dict2)
print("dict1 == dict2 ?",dict1 == dict2)
print("dict1 is dict2 ?",dict1 is dict2)

[1, 2, 3]
list1==list2 ? True
list1 is list2 ? False

{1, 2, 3}
set1==set2 ? True
set1 is set2 ? False

{1: [1, 'w'], 2: 0, 3: 98}
dict1 == dict2 ? True
dict1 is dict2 ? False

分析: 浅拷贝,为新变量重新分配一块内存,和原来变量的内存不一样,所以有

list1 is list2 ? False
set1 is set2 ? False
dict1 is dict2 ? False

但浅拷贝完,两个变量中的元素的值是一样的。

list1==list2 ? True
dict1 == dict2 ? True
set1==set2 ? True

2.2 对于列表

对于列表,还可以通过切片操作符“:”来完成浅拷贝

list1 = [1, 2, 3]
list2 = list1[:]
print(list2)
print("list1 == list2 ?",list1 == list2)
print("list1 is list2 ?",list1 is list2)

[1, 2, 3]
list1 == list2 ? True
list1 is list2 ? False

2.3 使用 copy.copy() 函数

函数 copy.copy() 函数,适用于任何数据类型

import copy

list1 = [1, 2, 3]
list2 = copy.copy(list1)
print(list2)
print("list1 == list2 ?",list1 == list2)
print("list1 is list2 ?",list1 is list2)

set1 = {1, 2, 3}
set2 = copy.copy(set1)
print(set2)
print("set1 == set2 ?",set1 == set2)
print("set1 is set2 ?",set1 is set2)

dict1 = {1:'xiaoming', 2:'xiahua',3:'xiaoli'}
dict2 = dict(dict1)
print(dict2)
print("dict1 == dict2 ?",dict1 == dict2)
print("dict1 is dict2 ?",dict1 is dict2)

[1, 2, 3]
list1 == list2 ? True
list1 is list2 ? False

{1, 2, 3}
set1 == set2 ? True
set1 is set2 ? False

{1: 'xiaoming', 2: 'xiahua', 3: 'xiaoli'}
dict1 == dict2 ? True
dict1 is dict2 ? False

2.4 对于元组

对于元组,使用 tuple() 或者切片操作符 ‘:’ 不会创建一份浅拷贝,相反它会返回一个指向相同元组的引用:

tuple1 = (1, 2, 3)
tuple2 = tuple(tuple1)
print(tuple2)
print("tuple1 == tuple2 ?",tuple1 == tuple2)
print("tuple1 is tuple2 ?",tuple1 is tuple2)

tuple1 = (1, 2, 3)
tuple2 = tuple1[:]
print(tuple2)
print("tuple1 == tuple2 ?",tuple1 == tuple2)
print("tuple1 is tuple2 ?",tuple1 is tuple2)

(1, 2, 3)
tuple1 == tuple2 ? True
tuple1 is tuple2 ? True

(1, 2, 3)
tuple1 == tuple2 ? True
tuple1 is tuple2 ? True

使用 tuple() 或者切片操作符 ‘:’ 不会创建一份浅拷贝,因为它开辟新的内存存储的是原对象的引用,而没有创建新的对象来存储原对象的子对象的引用,所以不是浅拷贝。相反它会返回一个指向相同元组的引用。

对字符串使用 str() 或者切片操作符 ‘:’,原理和 元组相同。

str1 = 'operation'
str2 = str1[:]
print(str2)
print("str1 == str2 ?",str1 == str2)
print("str1 is str2 ?",str1 is str2)

operation
str1 == str2 ? True
str1 is str2 ? True
str1 = 'operation'
str2 = str(str1)
print(str2)
print("str1 == str2 ?",str1 == str2)
print("str1 is str2 ?",str1 is str2)

operation
str1 == str2 ? True
str1 is str2 ? True
import copy

set1 = (1, 2, 3)
set2 = copy.copy(set1)
print(set2)
print("set1 == set2 ?",set1 == set2)
print("set1 is set2 ?",set1 is set2)

(1, 2, 3)
set1 == set2 ? True
set1 is set2 ? True
import copy

set1 = 'operation'
set2 = copy.copy(set1)
print(set2)
print("set1 == set2 ?",set1 == set2)
print("set1 is set2 ?",set1 is set2)

operation
set1 == set2 ? True
set1 is set2 ? True

也就是说,对字符串和元组使用 copy()、[:]、本身的构造器完成的复制,都只是开辟了内存存储原对象的引用,而不是存储原对象的子对象的引用。

2.5 关于切片操作符 ‘:’

切片操作符 ‘:’ 不能用于字典和集合完成浅拷贝

# set1 = {1, 2, 3}
# set2 = set1[:]

# dict1 = {1:1, 2:2, 3:3}
# dict2 = dict1[:]

2.6 和赋值的区别

和赋值的本质区别在于,赋值只是把原对象的引用给到新对象

set1 = {1:1, 2:2, 3:3}
set2 = set1
print(set2)
print("set1 == set2 ?",set1 == set2)
print(id(set1))
print(id(set2))

{1: 1, 2: 2, 3: 3}
set1 == set2 ? True
2515252654368
2515252654368

2.7 浅拷贝需注意的问题

对数据采用浅拷贝的方式时,如果原对象中的元素不可变,那倒无所谓;但如果元素可变,浅拷贝通常会出现一些问题,

list1 = [[1, 2], (30, 40)]
list2 = list(list1)
list1.append(100)
print("list1:",list1)
print("list2:",list2)
list1[0].append(3)
print("list1:",list1)
print("list2:",list2)
list1[1] += (50, 60)
print("list1:",list1)
print("list2:",list2)

list1: [[1, 2], (30, 40), 100]
list2: [[1, 2], (30, 40)]
list1: [[1, 2, 3], (30, 40), 100]
list2: [[1, 2, 3], (30, 40)]
list1: [[1, 2, 3], (30, 40, 50, 60), 100]
list2: [[1, 2, 3], (30, 40)]

2、深拷贝

Python 中以 copy.deepcopy() 来实现对象的深度拷贝

import copy

list1 = [[1, 2], (30, 40)]
list2 = copy.deepcopy(list1)

list1.append(100)
print("list1:",list1)
print("list2:",list2)

list1[0].append(3)
print("list1:",list1)
print("list2:",list2)

list1[1] += (50, 60)
print("list1:",list1)
print("list2:",list2)

list1: [[1, 2], (30, 40), 100]
list2: [[1, 2], (30, 40)]
list1: [[1, 2, 3], (30, 40), 100]
list2: [[1, 2], (30, 40)]
list1: [[1, 2, 3], (30, 40, 50, 60), 100]
list2: [[1, 2], (30, 40)]
  • 95
    点赞
  • 415
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
深拷贝(deep copy)和浅拷贝(shallow copy)是Python中关于对象复制的两个概念。 浅拷贝是指创建一个新的对象,其内容是原始对象的引用。也就是说,新对象与原始对象共享内存地址,对其中一个对象的修改会影响到另一个对象。在Python中,可以使用`copy`模块的`copy()`函数或者对象的`copy()`方法进行浅拷贝深拷贝则是创建一个新的对象,完全复制原始对象及其所有嵌套对象的内容。也就是说,新对象与原始对象完全独立,互不影响。在Python中,可以使用`copy`模块的`deepcopy()`函数或者对象的`deepcopy()`方法进行深拷贝。 下面是一个简单的示例代码来说明深拷贝浅拷贝的区别: ```python import copy # 原始对象 original_list = [1, 2, [3, 4]] print("原始对象:", original_list) # 浅拷贝 shallow_copy_list = copy.copy(original_list) print("浅拷贝对象:", shallow_copy_list) # 修改浅拷贝对象 shallow_copy_list[2][0] = 5 print("修改浅拷贝对象后,原始对象:", original_list) print("修改浅拷贝对象后,浅拷贝对象:", shallow_copy_list) # 深拷贝 deep_copy_list = copy.deepcopy(original_list) print("深拷贝对象:", deep_copy_list) # 修改深拷贝对象 deep_copy_list[2][1] = 6 print("修改深拷贝对象后,原始对象:", original_list) print("修改深拷贝对象后,深拷贝对象:", deep_copy_list) ``` 输出结果为: ``` 原始对象: [1, 2, [3, 4]] 浅拷贝对象: [1, 2, [3, 4]] 修改浅拷贝对象后,原始对象: [1, 2, [5, 4]] 修改浅拷贝对象后,浅拷贝对象: [1, 2, [5, 4]] 深拷贝对象: [1, 2, [3, 4]] 修改深拷贝对象后,原始对象: [1, 2, [5, 4]] 修改深拷贝对象后,深拷贝对象: [1, 2, [3, 6]] ``` 可以看到,对浅拷贝对象的修改会影响到原始对象,而对深拷贝对象的修改不会影响原始对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值