Learn More →
src: chainsaw man
資芽北區2023 講師YuKai 洪郁凱
你應該要知道:
Dictionary 字典
用大括號來宣告
ex_dict = {"bday": 928, "name": "Kain"} #這裡以字串:數字為例 print(ex_dict)
用其他的方式來宣告
ex_dict = dict([["bday", 928], ["name", "Kain"]]) #傳入一個List/Tuple # 每個元素為一個鍵值對 可用List/Tuple
Dictionary 字典
用大括號來宣告
最大的特色是可以用數字之外的物件來取值
ex_dict = {"bday": 928} #這裡以字串為例 ex_list = [928] print(ex_list["bday"], ex_list[0])
Key-Value Pairs 鍵值對
Key-Value | Index |
---|---|
一月 -> Jan | 0 -> Jan |
二月 -> Feb | 1 -> Feb |
九月 -> Sep | 8 -> Sep |
mon_list = ["Jan", "Feb", "Mar", ..., "Sep"] mon_dict = {"一月": "Jan", "二月": "Feb", "三月": "Mar", "九月": "Sep"} print(mon_list[0], mon_dict["一月"])
萬一資料不是有順序的呢?
location_dict = {"台北": "Taipei", "桃園": "Taoyuan"}
ex_dict = {"Mutated": "變異的", "Crab": "螃蟹"} print(ex_dict["Mutated"]) #一個長得很像字典的範例 print(ex_dict["變異的"]) # 不能反向! print(ex_dict["Dior"]) # 不能找沒在字典裡面的
最後兩行會導致Key Error
都是因為字典內沒有對應Key導致
用三種方式宣告一個字典
keys_list = ["名字", "屬性", "顏色"] value_list = ["皮卡丘", "雷系", "顏色"]
用三種方式宣告一個字典
keys_list = ["名字", "屬性", "顏色"] value_list = ["皮卡丘", "雷系", "顏色"] # Hints: # 用大括號 # 用dict()及雙層list # (bonus)
Key值可以不只是字串
ex_tuple = (9, 2, 8) ex_dict = {ex_tuple: 928928} #用Tuple也可以 print(ex_dict[ex_tuple])
新增,修改,刪除,迭代
新增
info = {} info["Kain"] = 928 print(info["Kain"])
修改
info = {"Kain": 928} info["Kain"] = "B09" print(info["Kain"])
刪除
info = {"Kain":928} info.pop("Kain") print("Kain" in info)
刪除
info = {"Kain": 928} del info["Kain"] print("Kain" in info) # 確認 "Kain"-928 還在不在
全部刪除!
info = {"Kain":928, ""} info.clear()
迭代
calories = {"apple":243, "banana":132, "candy":432} for i in calories: print(i)
for會迭代Key:Value裡面的Key
迭代(Key)
calories = {"apple":243, "banana":132, "candy":432} for i in calories.keys(): print(i)
迭代(Value)
calories = {"apple":243, "banana":132, "candy":432} for i in calories.values(): print(i)
迭代(Key,Value)
calories = {"apple":243, "banana":132, "candy":432} for i in calories.items(): print(i)
會將 (Key,Value) 組合成Tuple來迭代
合併兩個字典
dict_a = {"學校": "CKHS", "年級": "10th"} dict_b = {"類組": "三", "校排": "10%"} dict_c = merge(dict_a,dict_b)??? #這是psudo-code 不要記
合併兩個字典
用update
用星號爆開
dict_c = {**dict_a, **dict_b} # c為合併後的字典 dict_a.update(dict_b) # a為合併後的字典 #暴雷:之後可以用星號 *list_a / **dict_a 炸開並傳入func當作參數
處理Key Error
What is Key Error
student = {} student["Kain"] = "B09" print(student["Jolin"])
先檢查再取值
if "Jolin" in student: print(student["Jolin"])
也可以用Method
print(student.get("Jolin"))
help(dict) # 按q退出
ans_dict = dict() for k,v in given_dict.item(): ans_dict[v] = k
ex_tuple = (928,) ex_list = [928,] ex_dict = {"bday": 928} ex_set = {928}
dict_a = {} set_a = set(["Vokda","Rum","Gin","Orange Juice"]) set_a.add("Tequila") set_a.remove("Orange Juice")
Union
set_a = {1, 2, 3, 5, 8, 13} set_b = {2, 3, 5, 7, 11, 13} print(set_a.union(set_b)) print(set_a | set_b) # Elegant!
Intersection
set_a = {1, 2, 3, 5, 8, 13} set_b = {2, 3, 5, 7, 11, 13} print(set_a.intersection(set_b)) print(set_a & set_b) # Elegant!
Difference
set_a = {1, 2, 3, 5, 8, 13} set_b = {2, 3, 5, 7, 11, 13} print(set_a.difference(set_b)) print(set_a-set_b) # Elegant!
Symmetric Difference
set_a = {1, 2, 3, 5, 8, 13} set_b = {2, 3, 5, 7, 11, 13} print(set_a.symmetric_difference(set_b)) print(set_a^set_b) # Elegant!
src: Spy X Family
雜湊函式(Hash Function)
hash(obj, /)
Return the hash value for the given object.
Two objects that compare equal must also have the same hash value, but the
reverse is not necessarily true.
f(x) = 9 x + 28
f(100) = 928
f(3) = ?
f(?) = 73
定義妥善的f需要有
e.g. \(\sin(x), \tan(x)\)
好的Hash Function需要有
e.g. SHA256 SHA512
f(x) = x%17 g(x) = 2x + 3
f(x) = x裡字母a的數量