标签:指定 deepcopy xtend 定位 [1] ever 切片 copy clear
1.列表切片names = ["fengxiaoli","Cx","#wang","3bin",["dudu","longlong"],"fengye","Cx"] print(names[0],names[1]) print(names[0:2]) print(names[:2]) print(names[-1]) print(names[-3:-1]) print(names[-3:]) print(names[0:-1:2]) #0开头,-1结尾,间隔2;0和-1可省
2.打印列表
print(names) for i in names: #遍历列表中的值 print(i)
3.增加
names.append("chenjia") #从列表最后增加 names.insert(1,"xiaoming") #制定位置增加 print(names)
4.修改
names[1]="daming" print(names)
5.删除
names.remove("daming") #指定字符删除 names.pop(1) #按索引号删除 names.clear() #删除整个列表 del names[0:3] #指定索引删除 print(names)
6.其他
names.reverse() #将列表中的值反向 print(names) print(names.count("Cx")) #统计Cx names2 = [1,2,3,4] names.extend(names2) #合并列表names和names2 names.sort() #排序
7.浅复制,深层列表数据和复制前指向同一内存地址
names3 = names.copy() names[1] = "cxcx" #更改names中数据后,names3只有第一层列表未受影响,二级列表改变,说明二级列表中names和names3指向同一内存地址 names[4][0] = "DUDU" print(names) print(names3) #输出结果 # ['fengxiaoli', 'cxcx', '#wang', '3bin', ['DUDU', 'longlong'], 'fengye', 'Cx'] # ['fengxiaoli', 'Cx', '#wang', '3bin', ['DUDU', 'longlong'], 'fengye', 'Cx']
8.深copy ,列表中所有数据复制到一个新的内存地址,当数据量太大时,慎用
import copy names = ["fengxiaoli","Cx","#wang","3bin",["dudu","longlong"],"fengye","Cx"] names3 = copy.deepcopy(names) #copy.copy()也是浅copy names[1] = "cxcx" #更改names列表中的数据,复制后的names3不受影响 names[4][0] = "DUDU" print(names) print(names3) #输出结果 # ['fengxiaoli', 'cxcx', '#wang', '3bin', ['DUDU', 'longlong'], 'fengye', 'Cx'] # ['fengxiaoli', 'Cx', '#wang', '3bin', ['dudu', 'longlong'], 'fengye', 'Cx']
标签:指定 deepcopy xtend 定位 [1] ever 切片 copy clear
原文地址:http://blog.51cto.com/fengxiaoli/2064250