# wk08_1026_元組_字典 許瀚仁
串列排序
串列常用的方法列表
元組(Tuple)
建立字典
字典取值
字典維護
* * *
## 【Inclass】
<pre>
{範例}
刪除指定串列元素
成績由大到小排序
</pre>
實作6
西元2021年是牛年。請你開發一個程式:
當使用者輸入他的出生西元年之後,畫面會顯示這個西元年的生肖。
```python
生肖 = ['鼠', '牛', '虎', '兔', '龍', '蛇', '馬', '羊', '猴', '雞', '狗', '豬']
in_year = int(input('輸入你的出生西元年:'))
i = (in_year - 2020) % 12
result = 生肖[i]
print('你的出生年為', in_year, '生肖為', result)
```
輸入你的出生西元年:2003
你的出生年為 2003 生肖為 羊
實作2
輸入喜歡的水果,直到Enter鍵結束,找尋fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"]水果串列是否包含此水果,
並顯示該水果是串列中的第幾項
```python
fruits = ["香蕉","蘋果","橘子","鳳梨","西瓜"]
```
```python
#刪除指定串列元素 /<remove>
fruits = ["香蕉","蘋果","橘子","鳳梨","西瓜"]
fruit = input("想找甚麼水果")
if fruits.count(fruit) > 0 :
i = fruits.index(fruit)
print("水果串列中包含此水果,在第", i+1, "位,索引值為", i)
else :
print("水果串列不包含此水果")
```
['蘋果', '柳丁', '葡萄', '西瓜', '西瓜']
```python
#成績由大排到小
scores = []
in_score = 0
while in_score != -1:
in_score = int(input("please enter score:"))
if in_score != -1:
scores.append(in_score)
scores.sort(reverse = True)
print(scores)
```
please enter score:80
please enter score:60
please enter score:45
please enter score:75
please enter score:100
please enter score:69
please enter score:44
please enter score:22
please enter score:60
please enter score:441
please enter score:-1
[441, 100, 80, 75, 69, 60, 60, 45, 44, 22]
```python
scores = [10,60,35,100,90]
scores_sorted = sorted(scores, reverse = True)
print("原始",scores, "排序後=", scores_sorted)
```
原始 [10, 60, 35, 100, 90] 排序後= [100, 90, 60, 35, 10]
{補充} list + while 練習
1. 計算總和
2. 找出最大值
3. 偶數過濾
4. 串列反轉
5. 查找元素
```python
#
list1 = [30,69,85,100,35]
list2 = list1
list1.reverse()
print("原始=", list2, "反轉後=",list1)
```
原始= [35, 100, 85, 69, 30] 反轉後= [35, 100, 85, 69, 30]
```python
#2. 找出最大值
list2 = [30,69,85,100,35]
#the_max = max(list2)
the_max = list2[0]
for n in list2:
if n > the_max :
the_max = n
print(the_max)
```
100
```python
#2. 找出最大值
list2 = [30,69,85,100,35]
the_min = list2[0]
for n in list2:
if n < the_min :
the_min = n
print(the_min)
```
30
{範例}
<pre>
血型個性查詢
輸入及查詢學生成績
keys 及 values 顯示世大運獎牌數
intems 顯示世大運獎牌數
</pre>
```python
個性_血型 = {"A" : "內向穩重","B" : "外向樂觀", "O" : "堅強自信", "AB" : "聰明自然" }
個性_血型["O"]
print(個性_血型["O"])
```
```python
個性_血型 = {"A" : "內向穩重","B" : "外向樂觀", "O" : "堅強自信", "AB" : "聰明自然" }
個性_血型["O"]
print(個性_血型.get("O"))
```
堅強自信
```python
個性_血型 = {"A" : "內向穩重","B" : "外向樂觀", "O" : "堅強自信", "AB" : "聰明自然" }
print(個性_血型)
```
{'A': '內向穩重', 'B': '外向樂觀', 'O': '堅強自信', 'AB': '聰明自然'}
* * *
## 【Afterclass practice】
### {綜合演練 ch06}
( B ) 1. 關於字典,下列何者敘述是錯誤的?
(A)以「鍵-值」對方式儲存 (B)資料依序排列
(C)可由「鍵」取得「值」 (D)資料隨機排列
* * *
( D ) 2. d={"香蕉":20, "蘋果":50},print(d[0]) 的結果為何?
(A)香蕉 (B)20 (C)50 (D)產生錯誤
* * *
( B ) 3. d={"香蕉":20, "蘋果":50},print(d["香蕉"]) 的結果為何?
(A)香蕉 (B)20 (C)50 (D)產生錯誤
* * *
( C ) 4. d={"香蕉":20, "蘋果":50},print(d.get("巴樂", 60)) 的結果為何?
(A)20 (B)50 (C)60 (D)None
* * *
( A ) 5. d={"香蕉":20, "蘋果":50},程式「d["巴樂"]=60」的作用為:
(A)新增資料 (B)修改資料 (C)取得資料 (D)刪除資料
* * *
( A ) 6. d={"香蕉":20, "蘋果":50},程式「d.clear()」的作用為:
(A)刪除所有元素 (B)刪除一個元素 (C)刪除字典 (D)以上皆非
* * *
( C ) 7. d={"香蕉":20, "蘋果":50},print("香蕉" in d) 的結果為何?
(A)20 (B)50 (C)True (D)False
* * *
( C ) 8. 下列哪一個功能可取得字典中所有「值」?
(A)in (B)keys (C)values (D)items
* * *
( D ) 9. 下列哪一個功能可取得字典中所有「鍵」及所有「值」?
(A)in (B)keys (C)values (D)items
* * *
( B ) 10. d={"香蕉":20},print(d.setdefault("巴樂")) 的結果為何?