name=["zhangyang","guyun","xiangpeng"] print(name[0],name[2]) zhangyang xiangpeng
name=["zhangyang","guyun","xiangpeng"] print(name[1:3]) #切片,起始位置包括,结束位置不包括,顾头不顾尾 "zhangyang","guyun","xiangpeng"
name=["zhangyang","guyun","xiangpeng"] print(name[-1])#取最后一个,从右开始数,负号代表从右开始的方向, xiangpeng
name=["zhangyang","guyun","xiangpeng"] print(name[-2]) guyun
取最后两个值 name=["zhangyang","guyun","xiangpeng","xia"] print(name[-1:-3]) 取值是空 [] 因为切片是从左向右数,这里是-的,所以写的要从右往左数
取最后两个值 name=["zhangyang","guyun","xiangpeng","xia"] print(name[-3:-1]) #要最后两个,冒号左边从哪里开始,右边是。负号是从右边开始,顾首不顾尾 [‘guyun‘, ‘xiangpeng‘]
name=["zhangyang","guyun","xiangpeng","xia"] print(name[-2:]) #要最后两个,冒号左边从哪里开始,右边是 [‘xiangpeng‘, ‘xia‘] 把-1省略掉才能取出来
name=["zhangyang","guyun","xiangpeng","xia"] print(name[0:3]) //可以省略0 print(name[:3]) [‘zhangyang‘, ‘guyun‘, ‘xiangpeng‘] 顾首不顾尾
追加: name=["zhangyang","guyun","xiangpeng","xia"] name.append("lei") print(name) [‘zhangyang‘, ‘guyun‘, ‘xiangpeng‘, ‘xia‘, ‘lei‘]
插入: name=["zhangyang","guyun","xiangpeng","xia"] name.append("lei") name.insert(1,"chen")#chen的位置就是1 name.insert(3,"xin") #插入位置,每次只能插入一个 print(name) [‘zhangyang‘, ‘chen‘, ‘guyun‘, ‘xin‘, ‘xiangpeng‘, ‘xia‘, ‘lei‘]
修改 name=["zhangyang","guyun","xiangpeng","xia"] name[2]="xied" print(name) [‘zhangyang‘, ‘guyun‘, ‘xied‘, ‘xia‘]
删除: 法1 name=["zhangyang","guyun","xiangpeng","xia"] name.remove("xia") print(name) [‘zhangyang‘, ‘guyun‘, ‘xiangpeng‘]
法2 name=["zhangyang","guyun","xiangpeng","xia"] del name[3] print(name) [‘zhangyang‘, ‘guyun‘, ‘xiangpeng‘]
name=["zhangyang","guyun","xiangpeng","xia"] name.pop() 不输下标,代表删除最后一个 print(name) [‘zhangyang‘, ‘guyun‘, ‘xiangpeng‘]
name=["zhangyang","guyun","xiangpeng","xia"] name.pop(2) 删除指定位置 print(name) [‘zhangyang‘, ‘guyun‘, ‘xia‘]
name=["zhangyang","guyun","xiangpeng","xia"] print(name.index("guyun"))索引 1
name=["zhangyang","guyun","xiangpeng","xia"] print(name[name.index("guyun")]) guyun 把所在的位置的名字打印出来
name=["zhangyang","guyun","guyun","xiangpeng","xia"] print(name.count("guyun")) #统计有多少个guyun 2
name=["zhangyang","guyun","guyun","xiangpeng","xia"] name.clear() 清除 print(name) []
name=["zhangyang","guyun","guyun","xiangpeng","xia"] name.reverse() 翻转 print(name) [‘xia‘, ‘xiangpeng‘, ‘guyun‘, ‘guyun‘, ‘zhangyang‘]
name=["zhangyang","guyun","guyun","xiangpeng","xia"] name.sort()排序 print(name) [‘guyun‘, ‘guyun‘, ‘xia‘, ‘xiangpeng‘, ‘zhangyang‘]
name=["zhangyang","guyun","guyun","xiangpeng","xia"] name2=[1,2,3,4] name.extend(name2) 合并,并且name2还存在 print(name) [‘zhangyang‘, ‘guyun‘, ‘guyun‘, ‘xiangpeng‘, ‘xia‘, 1, 2, 3, 4] [1, 2, 3, 4]
name=["zhangyang","guyun","guyun","xiangpeng","xia"] name2=name.copy()复制 print(name) print(name2) [‘zhangyang‘, ‘guyun‘, ‘guyun‘, ‘xiangpeng‘, ‘xia‘] [‘zhangyang‘, ‘guyun‘, ‘guyun‘, ‘xiangpeng‘, ‘xia‘]
原文地址:https://www.cnblogs.com/googlewang/p/10704574.html
时间: 2024-11-14 12:30:09