## 【wk07_1019】串列與元組 ## 【inclass practice】 ### 【ch05_串列與元組】 5.1 串列的使用 5.2 使用 for 迴圈讀取串列 5.3 串列搜尋與計次 5.4 串列元素新增與刪除 5.5 串列排序 5.6 串列常用的方法列表 5.7 元組(Tuple) ### {範例} 1. 串列出值設定 \<list1> 2. 使用 for 迴圈讀取串列元素 \<list2> 3. 利用迴圈配合索引讀取串列元素 \<list3> 4. 以串列計算班級成績 \<append1> 5. 刪除指定串列元素 \<remove> 6. 成績由大到小排序 \<sorted> ```python # 串列出值設定 <list> list1 = [] list2 = ["奇異果", "蘋果", "火龍果"] list3 = [["新加坡", "澳洲", "瑞士"], ["跳繩", "健走", "游泳"]] print(list1) print(list2) print(list3) ``` [] ['奇異果', '蘋果', '火龍果'] [['新加坡', '澳洲', '瑞士'], ['跳繩', '健走', '游泳']] ```python print("我最想去旅遊的地方: %s" % list3[0][0]) ``` 我最想去旅遊的地方: 新加坡 ```python print(list2[0:2:1]) ``` ['奇異果', '蘋果'] ```python # append # 在串列的最後增加一個元素 list2.append("芒果") print(list2) ``` ['奇異果', '蘋果', '火龍果', '芒果'] ```python # pop() # 拿掉串列的最後一個元素 list2.pop() ``` '芒果' ```python # pop(0) # 拿掉串列的第一個元素 list2.pop(0) ``` '奇異果' ```python print(list2) ``` ['蘋果', '火龍果'] ```python list2[0] = "梨子" print(list2) ``` ['梨子', '火龍果'] ```python list2.insert(0, "奇異果") print(list2) ``` ['奇異果', '梨子', '火龍果'] ```python # remove # 刪除指定串列元素 list2.remove('梨子') print(list2) ``` ['奇異果', '火龍果'] ```python list4 = [96, 85, 61] print(max(list4)) print(min(list4)) print(len(list4)) print(list4.count(61)) print(list4.index(61)) ``` 96 61 3 1 2 ```python # 使用 for 迴圈讀取串列元素 <list> print(list4) for n in list4: print(n * 3, end= ' ') ``` [96, 85, 61] 288 255 183 ```python for i in range(len(list4)): print(i, list4[i], sep =",") ``` 0,96 1,85 2,61 ```python list2 = ["奇異果", "蘋果", "火龍果"] for i in range(len(list2)): print(i, list2[i], sep =",") ``` 0,奇異果 1,蘋果 2,火龍果 ```python # 以串列計算班級成績 scores = [96, 85, 61, 100, 31, 74, 62, 44] total = 0 average = 0 n = 0 for score in scores: total = total + score n = n + 1 average = total/n print("總成績 : %3.0f , 平均成績 : %5.2f" % (total, average)) ``` 總成績 : 553 , 平均成績 : 69.12 ```python # 以串列計算班級成績 scores = [] inscore = 0 while inscore != -1: inscore = int(input("enter score, to end just enter nothing and enter")) if inscore != -1: scores.append(inscore) print(scores) ``` enter score, to end just enter nothing and enter 100 enter score, to end just enter nothing and enter 56 enter score, to end just enter nothing and enter 90 enter score, to end just enter nothing and enter -1 [100, 56, 90] ```python # 以串列計算班級成績 scores = [] inscore = 0 total = 0 average = 0 n = 0 while inscore != -1: inscore = int(input("enter score, to end just enter nothing and enter ")) if inscore != -1: scores.append(inscore) for score in scores: total = total + score n = n + 1 average = total/n print(scores) print("總成績 : %3.0f , 平均成績 : %5.2f" % (total, average)) ``` enter score, to end just enter nothing and enter 96 enter score, to end just enter nothing and enter 85 enter score, to end just enter nothing and enter 61 enter score, to end just enter nothing and enter 100 enter score, to end just enter nothing and enter 31 enter score, to end just enter nothing and enter 74 enter score, to end just enter nothing and enter 62 enter score, to end just enter nothing and enter 44 enter score, to end just enter nothing and enter -1 [96, 85, 61, 100, 31, 74, 62, 44] 總成績 : 553 , 平均成績 : 69.12 ## 【afterclass practice】 1. 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell ) 2. 教學影音 lesson 8 ### 綜合演練 #### 1. 執行下列程式,下列結果何者正確? list1 = [1, 2, 3, 4, 5] print(list1[0]) ( A ) 0 ( B ) 1 ( C ) 2 ( D ) [1, 2, 3, 4,5] ```python list1 = [1, 2, 3, 4, 5] print(list1[0]) ``` 1 #### 2. 執行下列程式,下列結果何者正確? list4 = ["香蕉", "蘋果", "橘子"] print(list4[-3]) ( A ) 香蕉 ( B ) 蘋果 ( C ) 橘子 ( D ) 錯誤,索引值超過範圍 ```python list4 = ["香蕉", "蘋果", "橘子"] print(list4[-3]) ``` 香蕉 #### 3. 執行下列程式,n 的值為多少? scores = [85, 79, 93] n=len(scores) ( A ) 0 ( B ) 1 ( C ) 2 ( D ) 3 ```python scores = [85, 79, 93] n = len(scores) print(n) ``` 3 #### 4. 執行下列程式,n 的值為多少? list1 = ["香蕉","蘋果","橘子"] n = list1.index("蘋果") ( A ) 0 ( B ) 1 ( C ) 2 ( D ) 3 ```python list1 = ["香蕉","蘋果","橘子"] n = list1.index("蘋果") print(n) ``` 1 #### 5. 執行下列程式,下列結果何者正確? list1 = ["香蕉","蘋果","橘子"] n = list1.count("西瓜") ( A ) n=0 ( B ) n=1 ( C ) n=2 ( D ) 出現錯誤 ```python list1 = ["香蕉","蘋果","橘子"] n = list1.count("西瓜") print(n) ``` 0 #### 6. 執行下列程式,下列結果何者正確? list1 = [1,2,3,4,5,6] m = list1.pop() n = list1.pop(2) ( A ) m=1, n=6 ( B ) m=2, n=3 ( C ) m=6, n=2 ( D ) m=6, n=3 ```python list1 = [1,2,3,4,5,6] m = list1.pop() n = list1.pop(2) print("m =", m, "," , "n =", n) ``` m = 6 , n = 3 #### 7. 執行下列程式,下列顯示結果何者正確? list1 = [1,2,3,4,5,6] list1.insert(-1, "愛") list1.insert(12, "台灣") print(list1[5]) ( A ) -1 ( B ) 12 ( C ) 愛 ( D ) 台灣 ```python list1 = [1,2,3,4,5,6] list1.insert(-1, "愛") list1.insert(12, "台灣") print(list1[5]) ``` 愛 #### 8. 執行下列程式,下列顯示結果何者正確? list1 = [1,2,3,4,5,6] del list1[1] print(list1) ( A ) [1,2,3,4,5,6] ( B ) [2,3,4,5,6] ( C ) [1,3,4,5,6] ( D ) [1,2,3,4,5] ```python list1 = [1,2,3,4,5,6] del list1[1] print(list1) ``` [1, 3, 4, 5, 6] #### 9. 執行下列程式,下列顯示結果何者正確? list1=[3,2,1,5] list1.reverse() print(list1) ( A ) [3,2,1,5] ( B ) [1,2,3,5] ( C ) [5,3,2,1] ( D ) [5,1,2,3] ```python list1=[3,2,1,5] list1.reverse() print(list1) ``` [5, 1, 2, 3] #### 10. 執行下列程式,下列顯示結果何者正確? list1=[3,2,1,5] list2=sorted(list1,reverse=True) print(list2) ( A ) [3,2,1,5] ( B ) [1,2,3,5] ( C) [5,3,2,1] ( D ) [5,1,2,3] ```python list1=[3,2,1,5] list2=sorted(list1,reverse=True) print(list2) ``` [5, 3, 2, 1] ### 教學影音 lesson 8 ```python list1 = [1, 2, 3, 4, 5] print(list1[1:4]) ``` [2, 3, 4] ```python list2 = range(5) print(list2) print(list(list2)) ``` range(0, 5) [0, 1, 2, 3, 4] ```python list3 = range(1, 6, 2) print(list3) print(list(list3)) ``` range(1, 6, 2) [1, 3, 5] ## 【self practice】 ```python # index = 列表.index(要尋找的元素) # 尋找特定元素在列表 (list) 中的索引位置的方法 fruits = ["蘋果", "香蕉", "橙子", "葡萄"] index = fruits.index("香蕉") print(index) ``` 1 ```python # 如果要確保元素是否存在於列表中,可以在使用 list.index() 之前使用 in 運算符進行檢查,或者使用 try 和 except 來處理可能的錯誤 if "蘋果" in fruits: index = fruits.index("蘋果") print(f"蘋果的索引位置為 {index}") else: print("蘋果不在列表中") # 或者使用 try 和 except 處理可能的錯誤 try: index = fruits.index("橘子") print(f"橘子的索引位置為 {index}") except ValueError: print("橘子不在列表中") ``` 蘋果的索引位置為 0 橘子不在列表中 ```python # count = list.count(value) # 計算列表中特定元素的出現次數 fruits = ['apple', 'banana', 'apple', 'cherry', 'apple'] apple_count = fruits.count('apple') print(apple_count) ``` 3 ```python # remove # 刪除指定串列元素 fruits = ["奇異果", "蘋果", "火龍果", "酪梨", "芒果", "西瓜", "藍莓"] n = input("輸入水果 ") if n in fruits: fruits.remove(n) print(fruits) else: print(n, "不在列表中") ``` 輸入水果 奇異果 ['蘋果', '火龍果', '酪梨', '芒果', '西瓜', '藍莓']