python列表的增删改查实例代码
程序员文章站
2024-01-07 12:42:34
names=["zhao00","qian01","sun02","li03","li03","li03","zhou04"]
#print(names[]) 打...
names=["zhao00","qian01","sun02","li03","li03","li03","zhou04"] #print(names[]) 打印错误 print(names)
增
names.append("wu05") #增加到最后 names.insert(1,"zheng06") #增加到指定位置
改
names[0]="zhao" #改
删
#names.remove("zheng06") #del names[5] #names.pop() # 没有下标,默认删掉最后一个 #names.pop(0) #print(names) #del names 把整个列表删除 #names.clear() 把列表的元素删除
查
print(names.index("qian01")) #查位置 print(names[names.index("qian01")])#并打印数据 print(names.count("li03"))#统计相同元素的个数
合并
names2=["01","02","03"] names.extend(names2) #合并
特殊的输出
print(names[0:3]) #左闭右开,等效于[:3] print(names[-1]) #取最后一个 print(names[-2:]) #取最后两个,[-2:0],[-2:-1]不行
整个列表的输出
.print(names) ---------- .for i in names: print(i) ---------- .print(names[0:-1:1]) #间隔一个输出print(names[0:-1:2])
以上这篇python列表的增删改查实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。