1 import copy 2 names = ["zhangyang", ‘guyun‘, ‘xiangpeng‘, [‘jack‘,‘tom‘], ‘xuliangchen‘] 3 names2 = names.copy() #浅copy,复制后改变原列表,copy后的列表不发生改变 4 5 6 names[2] = "祥鹏" 7 names.append("zhangshan") 8 9 print(names) 10 print(names2) 11 12 names3 = copy.deepcopy(names)#深copy,copy后原列表改变,copy的列表也会发生同样改变 13 print(‘this is names3:‘, names3)
浅copy的三种实现方式:
1 import copy 2 3 names = ["zhangyang", ‘guyun‘, ‘xiangpeng‘, [‘jack‘,‘tom‘], ‘xuliangchen‘] 4 5 #第一种方式 6 names2 = copy.copy(names) 7 #第二种 8 names3 = names[:] 9 #第三种 10 names4 = list(names)
时间: 2024-11-07 02:43:56