1 # dict字典 2 # 字典是一種組合數據, 沒有順序的組合數據, 數據以鍵值對形式出現 3 # 建立空字典1 4 d1 = {} 5 print(d1) 6 7 # 建立空字典2 8 d2 = dict() 9 print(d2) 10 11 # 建立有值得字典, 每一組數據都用冒號隔開, 每一對鍵值對用逗號隔開 12 d = {"one": 1, "two": 2, "three": 3} 13 print(d) 14 15 # 用dict建立內容字典1 16 d = dict({"one": 1, "two": 2, "three": 3}) 17 print(d) 18 19 # 用dict建立內容字典2 20 # 利用關鍵字參數 21 d = dict(one=1, two=2, three=3) 22 print(d) 23 24 25 d = dict([("one", 1), ("two", 2), ("three", 3)]) 26 print(d) 27 28 # a = 1 29 # b = 2 30 # c = 3 31 # d = 4 32 # list = [a, b, c, d] 33 # mydict = dict.fromkeys(list, "?") 34 # print(mydict) 35 36 # 字典的特徵 37 # -字典是序列類型, 可是是無序的序列, 因此沒有分片和索引 38 # -字典中的數據每一個都有鍵值對,即KV對 39 # -key:必須是可哈希的值,好比int,string, float, tuple, 可是list, set, dict不行 40 # -value:任何值 41 # 字典常見操做 42 # -訪問數據 43 d = {"one": 1, "two": 2, "three": 3} 44 # 注意訪問格式 45 # 中括號內飾鍵值 46 print(d["one"]) 47 48 d["one"] = "eins" 49 print(d) 50 51 # 刪除某個操做 52 # 使用del操做 53 del d["one"] 54 print(d) 55 56 # 成員檢查in, not in 57 # 成員檢查的是key內容 58 d = {"one": 1, "two": 2, "three":3} 59 if 2 in d: 60 print("value") 61 62 if "two" in d: 63 print("key") 64 65 if ("two", 2) in d: 66 print("kv") 67 68 69 d = dict([("one", 1), ("two", 2), ("three", 3)]) 70 # 遍歷在python2 和 3 中區別比較大, 代碼不通用 71 # 按key來使用for循環 72 for k in d: 73 print(k, "--", d[k]) 74 75 # 同上 76 for k in d.keys(): 77 print(k, "--", d[k]) 78 79 # 只訪問字典的值 80 for v in d.values(): 81 print(v) 82 83 # 注意如下特殊用法 84 for k, v in d.items(): 85 print(k, "--", v) 86 87 88 # 字典生成式 89 d = dict([("one", 1), ("two", 2), ("three", 3)]) 90 91 # 的常規字典生成 92 dd = {k: v for k, v in d.items()} 93 print(dd) 94 95 # 加限制條件的常規字典生成 96 dd = {k: v for k, v in d.items() if v % 2 == 0} 97 print(dd) 98 99 100 # 字典的相關函數 101 # 通用函數:len, max, min, dict 102 # *str(字典):返回字典的字符串格式 103 d = dict([("one", 1), ("two", 2), ("three", 3)]) 104 print(d) 105 print(str(d)) 106 107 108 # clear:清空字典 109 # items:返回字典的鍵值對組成的元組格式 110 d = dict([("one", 1), ("two", 2), ("three", 3)]) 111 # d = {'one': 1, 'two': 2, 'three': 3} 112 print(d) 113 i = d.items() 114 print(type(i)) 115 print(i) 116 117 118 # keys:返回字典的鍵組成的一個結構 119 k = d.keys() 120 print(type(k)) 121 print(k) 122 123 124 # values:同理, 一個可迭代的結構 125 v = d.values() 126 print(type(v)) 127 print(v) 128 129 130 # get :根據指定鍵返回相應的值, 好處是, 能夠設置默認值 131 d = dict([("one", 1), ("two", 2), ("three", 3)]) 132 print(d) 133 print(d.get("on333")) 134 135 # get 默認值是None, 能夠設置 136 print(d.get("on444", 100)) 137 138 # 一如下會報錯 139 # print(d["on333"]) 140 141 142 # fromkeys:使用指定的序列做爲鍵, 使用一個值做爲字典的全部的鍵的值 143 l = ["eins", "zwei", "dree"] 144 # 注意fromkeys倆個參數的類型 145 d = dict.fromkeys(l, "hahaha") 146 print(d) 147 148 149 150 ::: 151 {} 152 {} 153 {'one': 1, 'two': 2, 'three': 3} 154 {'one': 1, 'two': 2, 'three': 3} 155 {'one': 1, 'two': 2, 'three': 3} 156 {'one': 1, 'two': 2, 'three': 3} 157 1 158 {'one': 'eins', 'two': 2, 'three': 3} 159 {'two': 2, 'three': 3} 160 key 161 one -- 1 162 two -- 2 163 three -- 3 164 one -- 1 165 two -- 2 166 three -- 3 167 1 168 2 169 3 170 one -- 1 171 two -- 2 172 three -- 3 173 {'one': 1, 'two': 2, 'three': 3} 174 {'two': 2} 175 {'one': 1, 'two': 2, 'three': 3} 176 {'one': 1, 'two': 2, 'three': 3} 177 {'one': 1, 'two': 2, 'three': 3} 178 <class 'dict_items'> 179 dict_items([('one', 1), ('two', 2), ('three', 3)]) 180 <class 'dict_keys'> 181 dict_keys(['one', 'two', 'three']) 182 <class 'dict_values'> 183 dict_values([1, 2, 3]) 184 {'one': 1, 'two': 2, 'three': 3} 185 None 186 100 187 {'eins': 'hahaha', 'zwei': 'hahaha', 'dree': 'hahaha'}