# 列表增删改查
names=[]
# 存放多值,从左到右,有序存储,可以修改可变,无限大,任何内容,
# 增
names=["sihan","size","sisi","caona"]
names.append("peixi")
n=[1,2,3]
names.extend(n)
print(names)
names.insert(1,"zijun")
print(names)
names.insert(1,[5,6,7])
print(names)
# 删
names.pop() #返回删除的值,列表为空的情况下,报错
print(names)
del names[-1]
print(names)
names.remove("sisi") #删除第一个"sisi",没有的情况下会报错
names.clear()
print(names) #清空
# 改
names[0]="xiangheng"
print(names)
# 查
print(names.index("xiangheng")) #第一个值的索引
print(names.count("xiangheng"))
"xiangheng" in names
# 切片
names=["sihan","size","sisi","caona"]
print(names[1:3])
print(names[1:])
print(names[1:-2])
print(names[:])
print(names[-1])
print(names[-3:-1])
print(names[-3:])
# 步长
print(names[::2])
print(names[-1:-5:-1]) #步长为-1,反着取,从右往左
print(names[::-1])
#排序 反转
n="山重水复疑无路"
print(n[::-1])
names=["sihan","size","sisi","caona"]
print(names.reverse()) #反转
print(names.sort())#排序 按照ascii码排序,,并且必须是同类型的数据类型排序
for i in names:
print(i)
# 针对列表
names=["金角大王","黑姑娘","rain","eva","狗蛋","银角大王","eva","鸡头"]
# 用index方法返回第二个eva的索引值
names.index("eva",names.index("eva")+1)
#把列表通过切片的形式反转
print(names[::-1])
#打印列表中下标为奇数的值
for a,b in enumerate(names):
if a % 2==1:
print(names[a])
else:
pass
#通过inde方法x找到第二个eva,并将改为EVA
names[names.index("eva",names.index("eva")+1)]="EAV"