#######################################灰魔法: list類中提供的方法 列表#######################################python
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 5 # 參數 6 # 1. 原來值最後追加 7 # 對象.方法(..) # li對象調用append方法 8 li = [11, 22, 33, 22, 44] 9 # li.append(5) 10 # li.append("alex") 11 # li.append([1234,2323]) 12 # print(li) 13 14 # 2 清空列表 15 # li.clear() 16 # print(li) 17 18 # 3 拷貝,淺拷貝 19 # v = li.copy() 20 # print(v) 21 22 # 4. 計算元素出現的次數 23 # v = li.count(22) 24 # print(v) 25 26 # 5. 擴展原列表,參數:可迭代對象 27 # li = [11, 22, 33, 22, 44] 28 # li.append([9898,"不得了"]) 29 # print(li) 30 # 31 # li.extend([9898,"不得了"]) 32 # for i in [9898,"不得了"]: 33 # li.append(i) 34 # 35 # li.extend("不得了") 36 # print(li) 37 38 # 6. 根據值獲取當前值索引位置(左邊優先) 39 # li = [11, 22, 33, 22, 44] 40 # v= li.index(22) 41 # print(v) 42 43 # 7. 在指定索引位置插入元素 44 # li = [11, 22, 33, 22, 44] 45 # li.insert(0,99) 46 # print(li) 47 48 # 八、 刪除某個值(1.指定索引;2. 默認最後一個),並獲取刪除的值 49 # li = [11, 22, 33, 22, 44] 50 # v = li.pop() 51 # print(li) 52 # print(v) 53 54 # li = [11, 22, 33, 22, 44] 55 # v = li.pop(1) 56 # print(li) 57 # print(v) 58 59 # 9. 刪除列表中的指定值,左邊優先 60 # li = [11, 22, 33, 22, 44] 61 # li.remove(22) 62 # print(li) 63 # PS: pop remove del li[0] del li[7:9] clear 64 65 # 10 將當前列表進行翻轉 66 # li = [11, 22, 33, 22, 44] 67 # li.reverse() 68 # print(li) 69 70 # 11 列表的排序 71 # li = [11,44, 22, 33, 22] 72 # li.sort() 73 # li.sort(reverse=True) 74 # print(li) 75 ### 欠 76 # cmp 77 # key 78 # sorted
# 1. 列表格式
# 2. 列表中能夠嵌套任何類型
# 中括號括起來
# ,分割每一個元素
# 列表中的元素能夠是 數字,字符串,列表,布爾值..全部的都能放進去
# 「集合」,內部放置任何東西app
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 5 # 3. 6 # 索引取值 7 li = [1,23,212,45,67,89] 8 # print(li[3]) 9 # 4 切片,切片結果也是列表 10 # print(li[3:-1]) 11 12 # 5 for循環 13 # while循環 14 # for item in li: 15 # print(item) 16 17 # 列表元素,能夠被修改 18 19 # li = [1, 12, 9, "age", ["石振文", ["19", 10], "龐麥郎"], "alex", True] 20 # print(li) 21 22 ############## 6 索引 23 # 修改 24 # li[1] = 120 25 # print(li) 26 # li[1] = [11,22,33,44] 27 # print(li) 28 29 # 刪除,第一種方式 30 # print(li) 31 # del li[1] 32 # print(li) 33 34 ############## 7 切片 35 # 修改 36 # print(li) 37 # li[1:3] = [120,90] 38 # print(li) 39 40 # 刪除 41 # print(li) 42 # del li[2:6] 43 # print(li) 44 45 # 8 in 操做 46 # li = [1, 12, 9, "age", ["石振文", ["19", 10], "龐麥郎"], "alex", True] 47 # v1 = "石振文" in li 48 # print(v1) 49 # v2 = "age" in li 50 # print(v2) 51 ###### 列表中的元素, 52 53 # 9 操做 54 # li = [1, 12, 9, "age", ["石振文", ["19", 10], "龐麥郎"], "alex", True] 55 # print(li[4][1][0]) 56 57 # 10 轉換 58 # 字符串轉換列表 li = list("asdfasdfasdf"), 內部使用for循環 59 # s = "pouaskdfauspdfiajsdkfj" 60 # new_li = list(s) 61 # print(new_li) 62 63 # 列表轉換成字符串, 64 # 須要本身寫for循環一個一個處理: 既有數字又有字符串 65 # li = [11,22,33,"123","alex"] 66 # r = str(li) # '[11,22,33,"123","alex"]' 67 # print(r) 68 # s = "" 69 # for i in li: 70 # s = s + str(i) 71 # print(s) 72 73 # 直接使用字符串join方法:列表中的元素只有字符串 74 # li = ["123","alex"] 75 # v = "".join(li) 76 # print(v) 77 78 ### 補充:字符串建立後,不可修改 79 # v = "alex" 80 # v = v.replace('l','el') 81 # print(v) 82 83 # li = [11,22,33,44] 84 # li[0] 85 # li[0] = 999 86 87 # s = "alex" 88 # li[0] 89 # s[0] = "E" 90 # print(s) 91 # 列表,有序;元素能夠被修改
#########################################元組###########################################################spa
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 5 # 元組,元素不可被修改,不能被增長或者刪除 6 # tuple 7 # tu = (11,22,33,44,22,22,22) 8 # print(tu.count(22))#獲取指定元素在元組中出現的次數 9 # print(tu.index(22)) 10 11 # 1. 書寫格式 12 tu = (111,"alex",(11,22),[(33,44)],True,33,44,) 13 # 通常寫元組的時候,推薦在最後加入 , 14 # 元素不可被修改,不能被增長或者刪除 15 16 # 2. 索引 17 # v = tu[0] 18 # print(v) 19 20 # 3. 切片 21 # v = tu[0:2] 22 # print(v) 23 24 # 4. 能夠被for循環,可迭代對象 25 # for item in tu: 26 # print(item) 27 28 # 5. 轉換 29 s = "asdfasdf0" 30 li = ["asdf","asdfasdf"] 31 tu = ("asdf","asdf") 32 33 # v = tuple(s) 34 # print(v) 35 36 # v = tuple(li) 37 # print(v) 38 39 # v = list(tu) 40 # print(v) 41 42 # v = "_".join(tu) 43 # print(v) 44 45 # li = ["asdf","asdfasdf"] 46 # li.extend((11,22,33,)) 47 # print(li) 48 49 # 6.元組的一級元素不可修改/刪除/增長 50 tu = (111,"alex",(11,22),[(33,44)],True,33,44,) 51 # # 元組,有序。 52 # v = tu[3][0][0] 53 # print(v) 54 # v=tu[3] 55 # print(v) 56 # tu[3][0] = 567 57 # print(tu)
###################字典######################code
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 5 dic = { 6 "k1": 'v1', 7 "k2": 'v2' 8 } 9 # 1 根據序列,建立字典,並指定統一的值 10 # v = dict.fromkeys(["k1",123,"999"],123) 11 # print(v) 12 13 # 2 根據Key獲取值,key不存在時,能夠指定默認值(None) 14 # v = dic['k11111'] 15 # print(v) 16 # v = dic.get('k1',111111) 17 # print(v) 18 19 # 3 刪除並獲取值 20 # dic = { 21 # "k1": 'v1', 22 # "k2": 'v2' 23 # } 24 # v = dic.pop('k1',90) 25 # print(dic,v) 26 # k,v = dic.popitem() 27 # print(dic,k,v) 28 29 # 4 設置值, 30 # 已存在,不設置,獲取當前key對應的值 31 # 不存在,設置,獲取當前key對應的值 32 # dic = { 33 # "k1": 'v1', 34 # "k2": 'v2' 35 # } 36 # v = dic.setdefault('k1111','123') 37 # print(dic,v) 38 39 # 5 更新 40 # dic = { 41 # "k1": 'v1', 42 # "k2": 'v2' 43 # } 44 # dic.update({'k1': '111111','k3': 123}) 45 # print(dic) 46 # dic.update(k1=123,k3=345,k5="asdf") 47 # print(dic) 48 49 # 6 keys() 7 values() 8 items() get update 50 ########## 51 52 53 54 # 一、基本機構 55 # info = { 56 # "k1": "v1", # 鍵值對 57 # "k2": "v2" 58 # } 59 #### 2 字典的value能夠是任何值 60 # info = { 61 # "k1": 18, 62 # "k2": True, 63 # "k3": [ 64 # 11, 65 # [], 66 # (), 67 # 22, 68 # 33, 69 # { 70 # 'kk1': 'vv1', 71 # 'kk2': 'vv2', 72 # 'kk3': (11,22), 73 # } 74 # ], 75 # "k4": (11,22,33,44) 76 # } 77 # print(info) 78 79 #### 3 列表、字典不能做爲字典的key 80 # info ={ 81 # 1: 'asdf', 82 # "k1": 'asdf', 83 # True: "123", 84 # # [11,22]: 123, 85 # (11,22): 123, 86 # # {'k1':'v1'}: 123 87 # 88 # } 89 # print(info) 90 91 # 4 字典無序 92 93 # info = { 94 # "k1": 18, 95 # "k2": True, 96 # "k3": [ 97 # 11, 98 # [], 99 # (), 100 # 22, 101 # 33, 102 # { 103 # 'kk1': 'vv1', 104 # 'kk2': 'vv2', 105 # 'kk3': (11,22), 106 # } 107 # ], 108 # "k4": (11,22,33,44) 109 # } 110 # print(info) 111 112 # 五、索引方式找到指定元素 113 # info = { 114 # "k1": 18, 115 # 2: True, 116 # "k3": [ 117 # 11, 118 # [], 119 # (), 120 # 22, 121 # 33, 122 # { 123 # 'kk1': 'vv1', 124 # 'kk2': 'vv2', 125 # 'kk3': (11,22), 126 # } 127 # ], 128 # "k4": (11,22,33,44) 129 # } 130 # v = info['k1'] 131 # print(v) 132 # v = info[2] 133 # print(v) 134 # v = info['k3'][5]['kk3'][0] 135 # print(v) 136 137 # 6 字典支持 del 刪除 138 # info = { 139 # "k1": 18, 140 # 2: True, 141 # "k3": [ 142 # 11, 143 # [], 144 # (), 145 # 22, 146 # 33, 147 # { 148 # 'kk1': 'vv1', 149 # 'kk2': 'vv2', 150 # 'kk3': (11,22), 151 # } 152 # ], 153 # "k4": (11,22,33,44) 154 # } 155 # del info['k1'] 156 # 157 # del info['k3'][5]['kk1'] 158 # print(info) 159 160 # 7 for循環 161 # dict 162 info = { 163 "k1": 18, 164 2: True, 165 "k3": [ 166 11, 167 [], 168 (), 169 22, 170 33, 171 { 172 'kk1': 'vv1', 173 'kk2': 'vv2', 174 'kk3': (11,22), 175 } 176 ], 177 "k4": (11,22,33,44) 178 } 179 # for item in info: 180 # print(item) 181 # 182 # for item in info.keys(): 183 # print(item) 184 # 185 # for item in info.values(): 186 # print(item) 187 188 # for item in info.keys(): 189 # print(item,info[item]) 190 191 # for k,v in info.items(): 192 # print(k,v) 193 194 # True 1 False 0 195 # info ={ 196 # "k1": 'asdf', 197 # True: "123", 198 # # [11,22]: 123 199 # (11,22): 123, 200 # # {'k1':' v1'}: 123 201 # 202 # } 203 # print(info)
####################### 整理 #################orm
# 1、數字
# int(..)
# 2、字符串
# replace/find/join/strip/startswith/split/upper/lower/format
# tempalte = "i am {name}, age : {age}"
# # v = tempalte.format(name='alex',age=19)
# v = tempalte.format(**{"name": 'alex','age': 19})
# print(v)
# 3、列表
# append、extend、insert
# 索引、切片、循環
# 4、元組
# 忽略
# 索引、切片、循環 以及元素不能被修改
# 5、字典
# get/update/keys/values/items
# for,索引對象
# dic = {
# "k1": 'v1'
# }blog
# v = "k1" in dic
# print(v)排序
# v = "v1" in dic.values()
# print(v)
# 6、布爾值
# 0 1
# bool(...)
# None "" () [] {} 0 ==> False索引