# Dictionary 2023/03/26 李慕庭 --- ## 函數 * $f(x) = y$ * 一個$x$只能對到一個$y$ * $x$ 不重複 ![函數](https://i.imgur.com/o82kYLE.png) [pic source](https://web.ntnu.edu.tw/~497403273/html/fx.htm) --- ## Dictionary * $D(key) = value$ * 一個$key$只能對到一個$value$ * $key$不重複 ---- * dictionary 中每個元素都由key & value 組成 * key只能是**不變**的資料型態 : * string, value, tuple.. * value可以是任何的資料型態 : * string, value, list, another dictionary... --- 1. 建立dictionary ```python= score = {"Fireball": 100, "Cookie": 60, "Chocolate":20} print(type(score)) print(score) ``` ---- ```python= cookies = dict(Chocolate=1, Mocha=2) print(cookies) ``` --- 2. 取得dictionary中的特定元素 傳入 $key$ 值 和list, string tuple 傳入位置index不同 ---- ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} print(score["Fireball"]) print(score["Cookie"]) print(score["Chocolate"]) ``` ---- :::spoiler 會有什麼問題? key error ::: ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} print(score["Mocha"]) ``` ---- ### How to solve key error - if ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} if "Mocha" in score: print(score["Mocha"]) ``` ---- ### How to solve key error - get() * 如果找不到這個key值就會回傳None * 也可以自訂找不到回傳的值 ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} print(score.get("Cookie")) # 60 print(score.get("Mocha")) # None print(score.get("Mocha", 0)) # 0 ``` --- 3. 遍歷所有dictionary中的元素 在list中 ```python= rank = [1, 3, 4, 6, 8] for i in range(5): print(rank[i]) for i in rank: print(i) ``` ---- :::spoiler 在dictionary中如果照做? 會只取到key值 因此底下的程式碼只會輸出 ``` Fireball Cookie Chocolate ``` ::: ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} for k in score: print(k) ``` ---- 用item來遍歷! ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} for k in score.items(): print(k) ``` ::: spoiler Output 回傳的元素型態是tuple 包含了一個key跟一個value ``` ('Fireball', 100) ('Cookie', 60) ('Chocolate', 20) ``` ::: --- # tuple ---- ## tuple 的特性 * 不可變動 -- 因此做為dictionary的$key$ * 所秏儲存空間較少 ---- 建立tuple ```python= score = 1, 2, 3 print(score) ``` ---- 如果想建立只有一個元素的tuple ```python= score1 = 1 print(score1) score2 = 1, print(score2) ``` ---- 也可以直接加上小括號來設tuple ```python= score = (1, 2, 3) print(score) ``` ---- 甚至是空的tuple ```python= nothing = () print(nothing) print(type(nothing)) ``` ---- 將其他資料型態轉換為tuple (string) ```python= name = "Fireball" tuple_name = tuple(name) print(type(tuple_name)) print(tuple_name) ``` ---- 將其他資料型態轉換為tuple (list) ```python= score = [1, 2, 3] tuple_score = tuple(score) print(type(tuple_score)) print(tuple_score) ``` --- 4. 新增元素至dictionary ```python= score = {"Fireball":100, "Cookie":60} print(score) score["Chocolate"] = 20 print(score) ``` --- 5. 更改dictionray中key對應到的值 ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} print(score) score["Fireball"] = 0 print(score) ``` --- 6. 刪除dictionary中的特定元素 ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} print(score) del score["Fireball"] print(score) ``` ---- 如果刪到不存在的key值 ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} del score["Mocha"] ``` ---- 也可以一次刪掉所有的元素 ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} score.clear() ``` --- ### 跟list哪裡不一樣 * 存取元素不是依照順序/index,而是key值 * 不像list能按照value來刪除 --- # 其他用法 ---- 輸出dictionary中元素數量 ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} print(len(score)) ``` ---- 輸出所有的key值 ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} print(score.keys()) ``` ---- 輸出所有的value值 ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} print(score.values()) ``` ---- 輸出所有的元素 ```python= score = {"Fireball":100, "Cookie":60, "Chocolate":20} print(score.items()) ``` ---- 所有元素按key排序輸出 ```python= score = {"Fireball":100, "Mocha":60, "Chocolate":20} print(sorted(score.items())) for k in sorted(score.items()): print(k) ``` ---- 所有元素按value排序輸出 ```python= score = {"Fireball":100, "Mocha":60, "Chocolate":20} print(sorted(score.items(), key=lambda x:x[1])) for k in sorted(score.items(), key=lambda x:x[1]): print(k) ``` --- 課後練習題 [NEOJ 3035](https://neoj.sprout.tw/problem/3035/) - 有好心的程式碼提示 [NEOJ 3036](https://neoj.sprout.tw/problem/3036/)
{"metaMigratedAt":"2023-06-17T23:26:32.933Z","metaMigratedFrom":"YAML","title":"Dictionary","breaks":true,"contributors":"[{\"id\":\"cc9d2950-af2b-4feb-8745-4367e4091769\",\"add\":4488,\"del\":174}]"}
    391 views