somepeople

python 深浅拷贝&赋值

1.赋值

两者完全一样

l1 = [1,2,[1,2,3]]
l2 = l1

print(l1,l2)
print(id(l1),id(l2))
print(id(l1[0]),id(l1[-1]),id(l1[-1][0]))
print(id(l2[0]),id(l2[-1]),id(l2[-1][0]))
# l1[-1].append(3)
l1[0]=5
print(l1,l2)
print(id(l1),id(l2))
print(id(l1[0]),id(l1[-1]),id(l1[-1][0]))
print(id(l2[0]),id(l2[-1]),id(l2[-1][0]))
l1[-1][0]=5
print(l1,l2)
print(id(l1),id(l2))
print(id(l1[0]),id(l1[-1]),id(l1[-1][0]))
print(id(l2[0]),id(l2[-1]),id(l2[-1][0]))
'''
[1, 2, [1, 2, 3]] [1, 2, [1, 2, 3]]
2362754916928 2362754916928
140736584533664 2362754916416 140736584533664
140736584533664 2362754916416 140736584533664
[5, 2, [1, 2, 3]] [5, 2, [1, 2, 3]]
2362754916928 2362754916928
140736584533792 2362754916416 140736584533664
140736584533792 2362754916416 140736584533664
[5, 2, [5, 2, 3]] [5, 2, [5, 2, 3]]
2362754916928 2362754916928
140736584533792 2362754916416 140736584533792
140736584533792 2362754916416 140736584533792
'''

2.浅拷贝

对于列表来说,第一层不可变元素的改变不会影响另一个,但可变元素的子元素改变时,另一个随之改变;浅拷贝等于生成了一个新容器,但容器内的元素的内存地址还是指向原来的

#浅拷贝是把原列表第一层的内存地址不加区分完全copy一份给新列表

l1 = [1,2,[1,2,3]]
l2 = l1.copy()

print(l1,l2)
print(id(l1),id(l2))
print(id(l1[0]),id(l1[-1]),id(l1[-1][0]))
print(id(l2[0]),id(l2[-1]),id(l2[-1][0]))
# l1[-1].append(3)
l1[0]=5
print(l1,l2)
print(id(l1),id(l2))
print(id(l1[0]),id(l1[-1]),id(l1[-1][0]))
print(id(l2[0]),id(l2[-1]),id(l2[-1][0]))
l1[-1][0]=5
print(l1,l2)
print(id(l1),id(l2))
print(id(l1[0]),id(l1[-1]),id(l1[-1][0]))
print(id(l2[0]),id(l2[-1]),id(l2[-1][0]))
'''
[1, 2, [1, 2, 3]] [1, 2, [1, 2, 3]]
2971505058432 2971505129472
140736584533664 2971505057920 140736584533664
140736584533664 2971505057920 140736584533664
[5, 2, [1, 2, 3]] [1, 2, [1, 2, 3]]
2971505058432 2971505129472
140736584533792 2971505057920 140736584533664
140736584533664 2971505057920 140736584533664
[5, 2, [5, 2, 3]] [1, 2, [5, 2, 3]]
2971505058432 2971505129472
140736584533792 2971505057920 140736584533792
140736584533664 2971505057920 140736584533792
'''

3.深拷贝,把可变类型和不可变类型元素区分对待,不可变元素拷贝后id不变,可变元素id改变;深拷贝使新生成的列表和旧列表的改操作完全隔离,不会相互影响

import copy
l1 = [1,2,[1,2,3]]
l2 = copy.deepcopy(l1)

print(l1,l2)
print(id(l1),id(l2))
print(id(l1[0]),id(l1[-1]),id(l1[-1][0]))
print(id(l2[0]),id(l2[-1]),id(l2[-1][0]))
# l1[-1].append(3)
l1[0]=5
print(l1,l2)
print(id(l1),id(l2))
print(id(l1[0]),id(l1[-1]),id(l1[-1][0]))
print(id(l2[0]),id(l2[-1]),id(l2[-1][0]))
l1[-1][0]=5
print(l1,l2)
print(id(l1),id(l2))
print(id(l1[0]),id(l1[-1]),id(l1[-1][0]))
print(id(l2[0]),id(l2[-1]),id(l2[-1][0]))
'''
[1, 2, [1, 2, 3]] [1, 2, [1, 2, 3]]
2913013588608 2913013659584
140736584533664 2913013588096 140736584533664
140736584533664 2913013658240 140736584533664
[5, 2, [1, 2, 3]] [1, 2, [1, 2, 3]]
2913013588608 2913013659584
140736584533792 2913013588096 140736584533664
140736584533664 2913013658240 140736584533664
[5, 2, [5, 2, 3]] [1, 2, [1, 2, 3]]
2913013588608 2913013659584
140736584533792 2913013588096 140736584533792
140736584533664 2913013658240 140736584533664
'''

 

posted on 2021-01-18 23:43  Hello_Thanos  阅读(73)  评论(0编辑  收藏  举报

导航