python列表的增删改查实例代码
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])
相关推荐
坚持是一种品质 2020-11-16
zhaobig 2020-11-16
文山羊 2020-10-31
kikaylee 2020-10-31
oXiaoChong 2020-10-30
PYTandFA 2020-09-15
CloudStrife0 2020-08-23
hang0 2020-08-16
lzujiangge 2020-08-16
RocNg 2020-08-15
xiesheng 2020-08-06
Tristahong 2020-08-05
georgeandgeorge 2020-08-03
elizabethxxy 2020-07-27
flycony 2020-07-05
柠檬班 2020-07-04
magicandy 2020-07-04
fanhuasijin 2020-06-27