python deepcopy性能_Python3中的深浅拷贝(copy()和deepcopy())

Python3中变量是对象的引用,即变量指向对象的内存地址;

1:变量赋值

在给变量赋值时会执行如下步骤:

如:a = 100

b3863ab9b311

image.png

在把变量赋值给变量的时候,最终结果就是两个变量指向相同的对象。

a = 100 之后执行 b=a,其过程如下:

b3863ab9b311

image.png

赋值操作不能拷贝一个对象,只是把两个变量指向了相同的对象;

2:拷贝

Python中像上面这种小的整数是没法拷贝的,因为整数是不可变数据类型以及python中的缓存机制;

下面以列表为例进行说明:

赋值:

# 赋值

print('--赋值---')

print('1-----:id(lst1)=',id(lst1),lst1)

print('2-----:id(lst2)=',id(lst2),lst2)

# 不管修改谁,修改的都是同一个对象

lst1[0] = 10

lst2[1] = 20

print('3-----:id(lst1)=',id(lst1),lst1)

print('4-----:id(lst2)=',id(lst2),lst2)

"""

--赋值---

1-----:id(lst1)= 1440098380744 [1, 2, 3, [4, 5, 6]]

2-----:id(lst2)= 1440098380744 [1, 2, 3, [4, 5, 6]]

3-----:id(lst1)= 1440098380744 [10, 20, 3, [4, 5, 6]]

4-----:id(lst2)= 1440098380744 [10, 20, 3, [4, 5, 6]]

"""

浅拷贝:

lst1 = [1,2,3,[4,5,6]]

lst2 = lst1[:] # [:] 也是拷贝

lst3 = lst1.copy() # copy()函数 拷贝

print('1-----:id(lst1)=',id(lst1),lst1)

print('2-----:id(lst2)=',id(lst2),lst2)

print('3-----:id(lst3)=',id(lst3),lst3)

## 不管修改谁,只修改自己指向的对象

lst1[0] = 10

lst2[1] = 20

lst3[2] = 30

print('4-----:id(lst1)=',id(lst1),lst1)

print('5-----:id(lst2)=',id(lst2),lst2)

print('6-----:id(lst2)=',id(lst3),lst3)

"""

--浅拷贝---

1-----:id(lst1)= 2752690027400 [1, 2, 3, [4, 5, 6]]

2-----:id(lst2)= 2752690027336 [1, 2, 3, [4, 5, 6]]

3-----:id(lst3)= 2752690027464 [1, 2, 3, [4, 5, 6]]

4-----:id(lst1)= 2752690027400 [10, 2, 3, [4, 5, 6]]

5-----:id(lst2)= 2752690027336 [1, 20, 3, [4, 5, 6]]

6-----:id(lst2)= 2752690027464 [1, 2, 30, [4, 5, 6]]

"""

深拷贝:

print('--深拷贝---')

lst1 = [1,2,3,[4,5,6]]

lst2 = lst1[:] # [:] 也是拷贝

lst3 = lst1.copy() # copy()函数 拷贝

print('1-----:id(lst1)=',id(lst1),lst1)

print('2-----:id(lst2)=',id(lst2),lst2)

print('3-----:id(lst3)=',id(lst3),lst3)

print('*'*10,'修改列表中嵌套的列表:')

# 修改列表中嵌套的列表,只要修改一个,其结果都会影响都其他两个,也就是说没有把里面嵌套的拷贝出来。

lst1[3][0] = 100

print('4-----:id(lst1)=',id(lst1),lst1)

print('5-----:id(lst2)=',id(lst2),lst2)

print('6-----:id(lst2)=',id(lst3),lst3)

print('*'*10,'使用deepcopy拷贝:')

import copy

lst1 = [1,2,3,[4,5,6]]

lst2 = copy.deepcopy(lst1)

# lst1,lst2是两个完全独立的对象,不管修改谁,都不会影响到另外一个。

lst1[0] = 'lst1'

lst2[0] = 'lst2'

lst1[3][0] = 100

lst2[3][0] = 200

print('7-----:id(lst1)=',id(lst1),lst1)

print('8-----:id(lst2)=',id(lst2),lst2)

"""

--深拷贝---

1-----:id(lst1)= 2147883248520 [1, 2, 3, [4, 5, 6]]

2-----:id(lst2)= 2147883248648 [1, 2, 3, [4, 5, 6]]

3-----:id(lst3)= 2147883248584 [1, 2, 3, [4, 5, 6]]

********** 修改列表中嵌套的列表:

4-----:id(lst1)= 2147883248520 [1, 2, 3, [100, 5, 6]]

5-----:id(lst2)= 2147883248648 [1, 2, 3, [100, 5, 6]]

6-----:id(lst2)= 2147883248584 [1, 2, 3, [100, 5, 6]]

********** 使用deepcopy拷贝:

7-----:id(lst1)= 2147883247496 ['lst1', 2, 3, [100, 5, 6]]

8-----:id(lst2)= 2147883248520 ['lst2', 2, 3, [200, 5, 6]]

"""

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值