标签:copy style print import 方法 int pre 切片 code
#列表的copy方法 names = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘] names2 = names.copy() print(names) print(names2) names[2] = ‘CC‘ print(names) print(names2) #浅copy names = [‘a‘, ‘b‘, [1, 2, 3], ‘d‘] names2 = names.copy() names[1] = ‘BB‘ names[2][1] = 22 print(names) print(names2) #直接copy names = [‘a‘, ‘b‘, [1, 2, 3], ‘d‘] names2 = names names[1] = ‘BB‘ names[2][1] = 22 print(names) print(names2) #完全copy一份列表 import copy #浅copy, =names.copy() names = [‘a‘, ‘b‘, [1, 2, 3], ‘d‘] names2 = copy.copy(names) names[1] = ‘BB‘ names[2][1] = 22 print(names) print(names2) #深copy names = [‘a‘, ‘b‘, [1, 2, 3], ‘d‘] names2 = copy.deepcopy(names) names[1] = ‘BB‘ names[2][1] = 22 print(names) print(names2) #列表循环 for i in names: print(i) #切片步长 names = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘] print(names) print(names[0:-1:2]) print(names[::2])
标签:copy style print import 方法 int pre 切片 code
原文地址:https://www.cnblogs.com/python-abc/p/12021751.html