列表list的增删改查
# 增 # lst.append("臭袜子") # 追加,在最后添加内容 ***** # print(lst) # lst.insert(1,"张达") # 插入 #消耗性能 **** # print(lst) # lst.extend("123") # 迭代添加 *** # print(lst) # 删 # lst = [1,2,3,4] # 容器 # print(lst.pop(0)) # 弹出 默认删除最后一个,pop返回的是被删除的元素,通过索引进行删除 # print(lst) # lst.remove(3) # 通过元素名称进行删除 # print(lst) # lst.clear() # 清空 # print(lst) # del lst # del - 垃圾回收机制 删除整个列表 # print(lst) # del lst[0] # 通过索引删除 # print(lst) # lst = [1,2,3,4] # 容器 # 通过切片删除 # del lst[0:2] # print(lst) # lst = [1,2,3,4] # 容器 # 通过步长删除 # del lst[0:4:2] # print(lst) # 改 # lst = [1,2,3,4,5] # 容器 # 方法一: # lst.pop(1) # lst.insert(1,"鬼谷子") # print(lst) # lst[1] = "郎的诱惑" ***** # print(lst) # lst[1:-1] = "1" # 可变长 **** # print(lst) # lst = [1,2,3,4,5] # 容器 # lst[1:-1] = "11",'2',"3" # lst[1:-1] = ('11', '2', '3') # print(lst) # lst = [1,2,3,4,5] # 容器 # lst[1:-1] = 123, # lst[1:-1] = (123,) # print(lst) # lst = [1,2,3,4,5] # 容器 # lst[1:-1] = "可迭代对象" # print(lst) # lst = [1,2,3,4,5] # 容器 # lst[1:-1:2] = "乐蝶" # 如果步长不为1识,修改的内容长度必须和要要修改的位置一致 # print(lst) # 查 # lst = [12,6,3,4,5] # 1.lst[0] # for循环 # for i in lst: # print(i) # 列表的嵌套 -- 索引 # 元组 # range -- 切片
列表的嵌套
# lst = [["蔡徐坤",["鸡你太美"],"跳","rap","篮球"], # ["鹿晗","小鲜肉","上海堡垒","甜蜜暴击"], # ["吴亦凡",["大碗宽面"],"加拿大炮王"], # ["周琦","大魔王"], # ["岳云鹏","五环","我的天哪","你好骚啊"], # ["于谦","抽烟","喝酒","烫头"], # ["高圆圆",["我们结婚吧",["倚天屠龙记",["金毛",["谢逊",["张无忌",["灭绝师太",["周芷若",["宋青书",["宋元桥","张三丰"]]]]]]]],"搜索"], # ["赵又厅","夜华"]], # ["刘涛",["欢乐颂","琅琊榜","还珠格格"]], # ["刘亦菲",["小龙女","天龙八部","仙剑","四大名捕"],["过儿","断臂","大雕","大宝剑"]] # ] # 倚天屠龙记 # print(lst[6][1][1][0]) 通过索引找到 # "琅琊榜" # print(lst[7][1][1]) 通过索引找到
# 3.使用while循环和for循环分别打印50以内的偶数 # 4.使用while循环和for循环分别打印50以内的奇数 # count = 0 # lst =list(range(0,51,2)) # while count < len(lst): # print(lst[count]) # count += 1 # for i in range(0,51,2): # print(i) # count = 0 # lst =list(range(1,51,2)) # while count < len(lst): # print(lst[count]) # count += 1 # for i in range(1,51,2): # print(i)
来源:https://www.cnblogs.com/biyunfeng/p/11934570.html