from copy import copy, deepcopy
l1 = [1,[2,3,4],{20,40,89},4,5]
赋值
l2 = l1
l2[0]=999
print('l1:')
print(l1)
print('l2:')
print(l2)
l2[1][0]=898
print('l1:')
print(l1)
print('l2:')
print(l2)
浅拷贝
l3 = copy(l1)
l3[0] = 786
print('l1:')
print(l1)
print('l3:')
print(l3)
结果是
l1:[1, [2, 3, 4], {40, 89, 20}, 4, 5]
l3:[786, [2, 3, 4], {40, 89, 20}, 4, 5]
(符合预期)
l3[1][0] = 9804
print('l1:')
print(l1)
print('l3:')
print(l3)
结果是
l1:[1, [9804, 3, 4], {40, 89, 20}, 4, 5]
l3:[1, [9804, 3, 4], {40, 89, 20}, 4, 5]
(不符合预期)
l3[1][0] = 9804
print('l1:')
print(l1)
print('l3:')
print(l3)
结果是
l1:[1, [9804, 3, 4], {40, 89, 20}, 4, 5]
l3:[1, [9804, 3, 4], {40, 89, 20}, 4, 5]
深拷贝
l3 = deepcopy(l1)
l3[1][0] = 9804
print('l1:')
print(l1)
print('l3:')
print(l3)