转载自:https://blog.csdn.net/weixin_43283397/article/details/104315148
实例代码:
import copy
List1 = ['1', '2', 3, 'a', ['b', 'c']]
List2 = List1 # 将List1赋给List2
List3 = copy.copy(List1) # 浅拷贝
List4 = copy.deepcopy(List1) # 深拷贝
List1.append('test') # 在List1末尾添加'test'
List1[4].append('d') # 在List1中['b','c']的末尾添加'd'
print('List1:%s' % List1)
print('List2:%s' % List2)
print('List3:%s' % List3)
print('List4:%s' % List4)
代码结果
List1:['1', '2', 3, 'a', ['b', 'c', 'd'], 'test']
List2:['1', '2', 3, 'a', ['b', 'c', 'd'], 'test']
List3:['1', '2', 3, 'a', ['b', 'c', 'd']]
List4:['1', '2', 3, 'a', ['b', 'c']]