#复制列表,浅copy names = [‘ZhangYang‘,‘ZhaoYi‘,‘ShaoYiFan‘,‘JiaChen‘,‘MengLingJun‘,‘LiuLin‘,[‘YangRui‘]] names2 = names.copy() print (names,names2) import copy #复制列表,深copy names2 = copy.deepcopy(names) names[3] = ‘汤姆‘ names[-1][0] = ‘杰克‘ print (names) print (names2)
import copy #浅copy,3中方式 person = [‘name‘,[‘saving‘,100]] ‘‘‘ p1 = copy.copy(person) p2 = person[:] p3 = list(person) ‘‘‘ p1 = person[:] p2 = person[:] p1[0] = ‘tom‘ p2[0] = ‘jane‘ p1[1][1] = 50 print (p1) print (p2)
时间: 2024-11-08 21:11:44