# 資研 1/5 社課講義 [**提問表單**](https://forms.gle/z4dPC1JPV6uA2q9w7) --- ## 字典 Dictionaries ### 迭代 #### <font color='#004B97'>**items**</font> ```python= D = {'一': 'one', '二': 'Two', '三': 3, '四': 'Four', '五':5} for i in D.items(): print(i) # 輸出:('一', 'one') # ('二', 'Two') # ('三', 3) # ('四', 'Four') # ('五', 5) ``` #### <font color='#004B97'>**keys**</font> ```python= D = {'一': 'one', '二': 'Two', '三': 3, '四': 'Four', '五':5} for i in D.keys(): print(i) # 輸出:一 # 二 # 三 # 四 # 五 ``` #### <font color='#004B97'>**values**</font> ```python= D = {'一': 'one', '二': 'Two', '三': 3, '四': 'Four', '五':5} for i in D.values(): print(i) # 輸出:one # Two # 3 # Four # 5 ``` ### 巢狀字典 Nested Dictionaries :::warning **巢狀迴圈 Nested Loop** - 雙重迴圈(迴圈中的迴圈) - **記得使用不同變數** ```python= # 印出"九九乘法表" for i in range(1, 10): for j in range(1, 10): print(f'{i}*{j}={i*j}', end='\t') print() ``` ::: 宣告法一 ```python= Members = { 'num_1':{ 'age': 17, 'birth': '一月三十日' }, 'num_2':{ 'age': 18, 'birth': '四月十日' } } print(Members['num_1']['age']) # 輸出:17 ``` 宣告法二 ```python= num_1 = { 'age': 17, 'birth': '一月三十日' } num_2 = { 'age': 18, 'birth': '四月十日' } Members = { 'num_1': num_1, 'num_2': num_2 } print(Members['num_1']['age']) # 輸出:17 ``` ### 函式 | 函式寫法 | 用途 | |:---------------------------:|:------------------------------:| | `Dict.items()` | 取得整個字典 | | `Dict.keys()` | 取得所有key | | `Dict.get(key)` | 取得所指定key的value | | `Dict.values()` | 取得所有value | |`Dict.setdefault(key1, key2)`|回傳所指定key的value(可同時回傳多個)| | `Dict.update({key: value})` | 更改、新增字典中的資料 | | `Dict.pop(key)` | 刪除所指定key的該項資料 | | `Dict.popitem()` | 刪除隨機一項資料 | | `del Dict[key]` | 刪除所指定key的該項資料 | | `Dict.clear()` | 清除該字典內的所有資料 | | `len(Dict)` | 回傳該字典的長度 | | `dict.fromkeys(x, y)` | 回傳一個字典其key為x,value為y | ```python= # len D = {'一': 'one', '二': 'Two', '三': 3, '四': 'Four', '五':5} print(len(D)) # 輸出:5 # fromkeys x = ['Tom', 'Cindy', 'Chris'] # 可以是tuple也可以是list y = 60 D = dict.fromkeys(x, y) # 此時的dict為函式必須,非字典名稱 print(D) # 輸出:{'Tom': 60, 'Cindy': 60, 'Chris': 60} ``` ### 排序 1. 直接排序 >依照key去排序(ASCII碼) 法一 ```python= D = { 'Sam': 95, 'Molly': 80, 'Albert': 80, 'Cindy': 65, 'Sally': 20 } print(sorted(D)) # 輸出:['Albert', 'Cindy', 'Molly', 'Sally', 'Sam'] ``` 法二 ```python= D = { 'Sam': 95, 'Molly': 80, 'Albert': 80, 'Cindy': 65, 'Sally': 20 } print(sorted(D.keys())) # 輸出:['Albert', 'Cindy', 'Molly', 'Sally', 'Sam'] ``` 法三 ```python= D = { 'Sam': 95, 'Molly': 80, 'Albert': 80, 'Cindy': 65, 'Sally': 20 } print(sorted(D.items())) # 輸出:[('Albert', 80), ('Cindy', 65), ('Molly', 80), ('Sally', 20), ('Sam', 95)] ``` 2. 使用value來排序 :::warning **lambda** - 小型的自訂義函式(? - 語法: ```python= x = lambda a, b: a**b print(x(2, 5)) ``` ::: ```python= # 依value排序_由低到高 D = { 'Sam': 95, 'Molly': 80, 'Albert': 80, 'Cindy': 65, 'Sally': 20 } print(sorted(D.items(), key=lambda x:x[1])) # 輸出:[('Sally', 20), ('Cindy', 65), ('Molly', 80), ('Albert', 80), ('Sam', 95)] ``` ```python= # 依value排序_由高到低 D = { 'Sam': 95, 'Molly': 80, 'Albert': 80, 'Cindy': 65, 'Sally': 20 } print(sorted(D.items(), key=lambda x:-x[1])) # 輸出:[('Sam', 95), ('Molly', 80), ('Albert', 80), ('Cindy', 65), ('Sally', 20)] ``` ```python= # 依value排序_先按value降冪,若相同再按key升冪 D = { 'Sam': 95, 'Molly': 80, 'Albert': 80, 'Cindy': 65, 'Sally': 20 } print(sorted(D.items(), key=lambda x:(-x[1], x[0]))) # 輸出:[('Sam', 95), ('Albert', 80), ('Molly', 80), ('Cindy', 65), ('Sally', 20)] ``` ## 內建函式-數學函式 1. **abs()** - `abs( a )` - 回傳該數的絕對值 - 不改變資料型態 ```python= x = -123.0 print( abs(x) ) # 輸出:123.0 ``` 2. **pow()** - `pow( a, b, c )` - 回傳以a為底數;b為指數;%c的數值 >意同:( a \** b ) % c >a、b為必須參數;c可有可無 ```python= print( pow(2, 3, 3), (2**3)%3 ) # 輸出:2 2 ``` 3. **round()** - `round( a, b )` - 回傳將小數a,四捨五入至小數點後第b位 >a為必須參數;b可有可無 ```python= print( round(1.233333, 3) ) # 輸出:1.233 ``` 4. **min()** - `min( a, b , c ... )` - 回傳該容器或多個物件的最小值 - 只能比較同一資料型態的物件 >可放置int、float、list、tuple...等可迭代物件 ```python= print( min(1, -3, 44, 6, 8) ) # 輸出:-3 ``` 5. **max()** - `max( a, b , c ... )` - 回傳該容器或多個物件的最大值 - 只能比較同一資料型態的物件 >可放置int、float、list、tuple...等可迭代物件 ```python= print( max(1, -3, 44, 6, 8) ) # 輸出:44 ``` ## **練習題** **Zerojudge** [**連結**](https://zerojudge.tw/) --- ## 補充資料 **推薦網站:**[**W3Schools**](https://www.w3schools.com/)