"""
列表视图 vs 副本
"""
list1 = [1, 2, 3, 4]
#赋值,视图
list2 = list1
#复制,副本(浅复制)
list3 = list1.copy()
print(f'list1:{list1}')
print(f'list2:{list2}')
print(f'list3:{list3}')
print()
list2[0] = 'a'
list2[1] = 'b'
print(f'list1:{list1}')
print(f'list2:{list2}')
print(f'list3:{list3}')
print()
list3[2] = 'c'
list3[3] = 'd'
print(f'list1:{list1}')
print(f'list2:{list2}')
print(f'list3:{list3}')
"""
嵌套列表浅复制 vs 深复制
"""
import copy
list1 = [1, 2, 3, [4, 5]]
print('原始list')
print(list1)
# 深复制,适用于嵌套列表
list_deep = copy.deepcopy(list1)
# 只复制一层
list2 = list1.copy()
list3 = list1[:]
list4 = list(list1)
list5 = [*list1]
# 修改元素
list_deep[3][0] = 'deep'
list_deep[2] = 'worked_0'
list2[3][0] = 'abc'
list2[2] = 'worked_1'
list3[3][0] = 'X1'
list3[2] = 'worked_2'
list4[3][0] = 'X2'
list4[2] = 'worked_3'
list5[3][0] = 'X3'
list5[2] = 'worked_4'
print('新list')
print(list1)
print(list_deep)
print(list2)
print(list3)
print(list4)
print(list5)