# 列表的增加 append加入在末尾
hero = ["韩信","老夫子","LAN"]
hero.append("LB")
hero.append("元芳")
print(hero) # output:['韩信', '老夫子', 'LAN', 'LB', '元芳']
# 插入 插入到指定索引的位置
hero = ["韩信","老夫子","LAN"]
hero.insert(2,"yasuo")
print(hero) # output:['韩信', '老夫子', 'yasuo', 'LAN']
# 合并
hero = ["韩信","老夫子","LAN"]
LOL = ["luo","xia","eyu"]
hero.append(LOL)
print(hero) # output:['韩信', '老夫子', 'LAN', ['luo', 'xia', 'eyu']]
# 嵌套
hero = ["韩信","老夫子","LAN"]
hero.insert(2,["luo","xia","eyu"])
print(hero) # output: ['韩信', '老夫子', ['luo', 'xia', 'eyu'], 'LAN']
print(hero[2][1]) # output: xia
# 删除 del pop remove
hero = ["韩信","老夫子","LAN"]
del hero[2] # 删除指定索引上的值
print(hero) # output: ['韩信', '老夫子']
hero = ["韩信","老夫子","LAN"]
hero.pop() # 默认删除最后一个
print(hero) # output:['韩信', '老夫子']
hero.pop(1)
print(hero) # output:['韩信']
hero = ["韩信","老夫子","LAN","monkey","LAN"]
hero.remove("LAN") # 删除第一个查找到的值
print(hero) # output:['韩信', '老夫子', 'monkey', 'LAN']
# clear 清空
hero = ["韩信","老夫子","LAN","LAN"]
hero.clear()
print(hero) # output:[]
# 修改
hero = ["韩信","老夫子","LAN"]
hero[0] = "LB"
hero[-1] = "剑姬"
print(hero) # ['LB', '老夫子', '剑姬']
# 查找
hero = ["韩信","老夫子","LAN","老夫子"]
print(hero.index("LAN")) # 2
print(hero.count("老夫子")) # 2
item = "老夫子"
if item in hero: # 先判断在不在列表
n = hero.index(item) # 取索引
print(n)
hero[n] = "LB" #修改
print(hero) # ['韩信', 'LB', 'LAN', '老夫子']
|