list列表:
1 # li = [11, 22, 33, 22, 44] 2 # 参数 3 # 1. 原来值最后追加 4 # 对象.方法(..) # li对象调用append方法 5 # li.append(5) 6 # li.append("alex") 7 # li.append([1234,2323]) 8 # print(li) 9 # 2 清空列表 10 # li.clear() 11 # print(li) 12 13 # 3 拷贝,浅拷贝 14 # v = li.copy() 15 # print(v) 16 # 4. 计算元素出现的次数 17 # v = li.count(22) 18 # print(v) 19 20 # # 5. 扩展原列表,参数:可迭代对象 21 # li = [11, 22, 33, 22, 44] 22 # li.append([9898,"不得了"]) 23 # print(li) 24 # [11, 22, 33, 22, 44, [9898, '不得了']] 25 26 # li.extend([9898,"不得了"]) 27 # for i in [9898,"不得了"]: 28 # li.append(i) 29 # [11, 22, 33, 22, 44, 9898, '不得了'] 30 # 31 # li.extend("不得了") 32 # print(li) 33 34 # 6. 根据值获取当前值索引位置(左边优先) 35 # li = [11, 22, 33, 22, 44] 36 # v= li.index(22) 37 # print(v) 38 39 # 7. 在指定索引位置插入元素 40 # li = [11, 22, 33, 22, 44] 41 # li.insert(0,99) 42 # print(li) 43 44 # 8、 删除某个值(1.指定索引;2. 默认最后一个),并获取删除的值 45 # li = [11, 22, 33, 22, 44] 46 # v = li.pop() 47 # print(li) 48 # print(v) 49 50 # li = [11, 22, 33, 22, 44] 51 # v = li.pop(1) 52 # print(li) 53 # print(v) 54 # 9. 删除列表中的指定值,左边优先 55 # li = [11, 22, 33, 22, 44] 56 # li.remove(22) 57 # print(li) 58 # PS: pop remove del li[0] del li[7:9] clear 59 60 # 10 将当前列表进行翻转 61 # li = [11, 22, 33, 22, 44] 62 # li.reverse() 63 # print(li) 64 65 # 11 列表的排序 66 li = [11,44, 22, 33, 22] 67 li.sort() 68 # li.sort(reverse=True) 69 print(li)
相关操作:
1 # 修改 2 # li[1] = 120 3 # print(li) 4 # li[1] = [11,22,33,44] 5 # print(li) 6 7 # 删除,第一种方式 8 # del li[1] 9 # print(li) 10 ############## 7 切片 11 # 修改 12 # li[1:3] = [120,90] 13 # print(li) 14 # 删除 15 # del li[2:6] 16 # print(li) 17 18 # 8 in 操作 19 # li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True] 20 # v1 = "石振文" in li 21 # print(v1) 22 # v2 = "age" in li 23 # print(v2) 24 ###### 列表中的元素, 25 26 # 9 操作 27 # li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True] 28 # print(li[4][1][0]) 29 # li[4][1][0] 30 # [1] 31 32 # li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True] 33 34 # s = "pouaskdfauspdfiajsdkfj" 35 # s = 123 36 # a = "123" 37 # int(a) 38 # a = 123 39 # str(a) 40 # 10 转换 41 # 字符串转换列表 li = list("asdfasdfasdf"), 内部使用for循环 42 # s = "pouaskdfauspdfiajsdkfj" 43 # new_li = list(s) 44 # print(new_li) 45 46 # 列表转换成字符串, 47 # 需要自己写for循环一个一个处理: 既有数字又有字符串 48 # li = [11,22,33,"123","alex"] 49 # r = str(li) # '[11,22,33,"123","alex"]' 50 # print(r) 51 # s = "" 52 # for i in li: 53 # s = s + str(i) 54 # print(s) 55 # 直接使用字符串join方法:列表中的元素只有字符串 56 # li = ["123","alex"] 57 # v = "".join(li) 58 # print(v) 59 60 ### 补充:字符串创建后,不可修改 61 # v = "alex" 62 # v = v.replace('l','el') 63 # print(v) 64 65 # li = [11,22,33,44] 66 # li[0] 67 # li[0] = 999 68 69 # s = "alex" 70 # li[0] 71 # s[0] = "E" 72 73 # li = [11,22,33,44] 74 # print(li) 75 # print(li) 76 # print(li) 77 # print(li) 78 # print(li) 79 # print(li) 80 # print(li) 81 # print(li) 82 # 列表,有序;元素可以被修改 83 84 # 列表 85 # list 86 # li = [111,22,33,44]