# remove(要删除的值) # list.remove("你xx") list = ["小明", "小红", "你xx"] list.insert(2, "小张") list.append("ajaks") print(list) list2 = ["ssnaj", "anskan"] list.extend(list2) print(list) list[1] = "mkans" print(list) # del(列表(哪个值)) del(list[4]) print(list) list.remove("你xx") print(list)
# pop删除末尾数据 list = ["小明", "小红", "你xx"] list.pop() print(list)
# pop(),删除指定索引的值,功能于del类似 list = ["小明", "小红", "你xx"] list.pop(0) print(list)
# list.clear() 清空 list = ["小明", "小红", "你xx"] list.clear() print(list)
# count 统计成员相同数量 # count(值) # 如果有多个值,返回值的数量,如果没有值则返回0 list = ["刘备", "小红", "刘备", "666", "刘备"] print(list.count("小红")) print(list.count("666")) print(list.count("刘备")) print(list.count("aaa"))
# 返回指定值在列表中的索引编号 # index() list = ["刘备", "小红", "刘备", "666", "刘备"] # 从第二值开始统计刘备的数量 print(list.index("刘备", 1))
# sort() 升序 # sort(reverse=True) 降序 list = [1,23,123,144,311,1213,] list.sort() print(list) # 降序 list.sort(reverse=True) print(list)
# reverse # 逆置, 颠倒顺序,反转 list = [1,23,123,144,311,1213,] list.reverse() print(list)
# 定义一个列表变量,内容如下 # ["张飞","刘备","关羽","刘邦","刘老二","曹操",] # 把刘老二修改成周瑜 list = ["张飞","刘备","关羽","刘邦","刘老二","曹操"] list[4] = "周瑜" print(list)
# 定义一个空列表 # 向列表内添加, 5,9,13这三个数字 list = [] list.append(5) list.append(9) list.append(13) print(list)