## wk07_1019_list_tuple - 串列 1. 使用 2. 讀取 3. 搜尋 4. 新增/刪除 5. 排序 6. 常用方法 - 元組 ## 【inclass practice】 串列 - 提供儲存資料的記憶體空間 - 元素置於[]中括弧之間,以逗號分開 - 各元素資料型態可以相同,也可以不同 - 空串列 / 多維串列 - 串列操作 - 創建(宣告)串列 - 讀取元素 - 修改元素 - 刪除元素 - 新增元素 - 排序 - 搜尋 ### {概念複習} ### {綜合演練} #### 實作2 輸入喜歡的水果,直到Enter鍵結束,找尋fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"]水果串列是否包含此水果,並顯示該水果是串列中的第幾項 ```python fruits = ["香蕉","蘋果","橘子","鳳梨","s"] while True: fruit = input("請輸入喜歡的水果(Enter 結束):") if (fruit==""): break n = fruits.count(fruit) if (n>0): # 串列元素存在 p=fruits.index(fruit) print("%s 在串列中的第 %d 項" %(fruit,p+1)) else: print(fruit,"不在串列中!") ``` 請輸入喜歡的水果(Enter 結束):蘋果 蘋果 在串列中的第 2 項 請輸入喜歡的水果(Enter 結束):s s 在串列中的第 5 項 請輸入喜歡的水果(Enter 結束): #### 實作6 西元2021年是牛年。請你開發一個程式: 當使用者輸入他的出生西元年之後,畫面會顯示這個西元年的生肖。 ```python animals = "鼠牛虎免龍蛇馬羊猴雞狗豬" year = int(input("請輸入你的出生西元年:")) print("西元{}年出生屬{}".format(year, animals[(year-2020) % 12])) ``` 請輸入你的出生西元年:1966 西元1966年出生屬馬 ### {範例} #### 1. 串列出值設定 \<list1> #### 2. 使用 for 迴圈讀取串列元素 \<list2> #### 3. 利用迴圈配合索引讀取串列元素 \<list3> #### 4. 以串列計算班級成績 \<append1> #### 5. 刪除指定串列元素 \<remove> #### 6. 成績由大到小排序 \<sorted> ```python #串列出值設定 <list1> scores = [85, 79, 93] print("國文成績:%d 分" % scores[0]) print("數學成績:%d 分" % scores[1]) print("英文成績:%d 分" % scores[2]) ``` ```python #使用 for 迴圈讀取串列元素 <list2> items = [12, "Apple", True] for item in items: print(item) ``` ```python #利用迴圈配合索引讀取串列元素 <list3> subjects=["國文","數學","英文"] scores = [85, 79, 93] for i in range(len(scores)): # 即 for i in range(3): print("%s成績:%d 分" % (subjects[i],scores[i])) ``` ```python #以串列計算班級成績 <append1> scores = [] total = inscore = 0 while(inscore != -1): inscore = int(input("請輸入學生的成績:")) if (inscore!=-1): # 將成績加入 scores 串列中 scores.append(inscore) print("共有 %d 位學生" % (len(scores))) for score in scores: # 將成績累加 total += score average = total / (len(scores)) #求平均值 print("本班總成績:%d 分,平均成績:%5.2f 分" % (total, average)) ``` ```python #刪除指定串列元素 <remove> fruits = ["香蕉","蘋果","橘子","鳳梨","西瓜"] while True: print("串列元素有:",fruits) fruit = input("請輸入要刪除的水果(Enter 結束):") if (fruit==""): break n = fruits.count(fruit) if (n>0): # 串列元素存在 fruits.remove(fruit) else: print(fruit,"不在串列中!") ``` ```python #成績由大到小排序 <sorted> scores = [] while True: inscore = input("請輸入學生的成績:") if (inscore==""): break # 將成績轉為數值後加入 scores 串列中 scores.append(int(inscore)) scores2=sorted(scores,reverse=True) # 由大到小排序 print("成績由大到小排序:",scores2) # list1=[3,2,1,5] #[3, 2, 1, 5] list2=sorted(list1,reverse=True) print(list2) #[5, 3, 2, 1] print(list1) #[3, 2, 1, 5] ``` ```python #sequential num=[256,731,943,389,142,645,829,945] name=["林小虎","王中森","邵木淼","李大同","陳子孔","鄭美麗","曾溫柔","錢來多"] no = int(input("請輸入中獎者的編號:")) Isfound=False for i in range(len(name)): #逐一比對搜尋 if (num[i]==no): #號碼相符 Isfound=True #設旗標為 True break #結束比對 if (Isfound==True): print("中獎者的姓名為:",name[i]) else: print("無此中獎號碼!") print("共比對 %d次 " %(i+1)) ``` 請輸入中獎者的編號:945 中獎者的姓名為: 錢來多 共比對 8次 ```python #test list1 = [1,2,3,4,5,6] del list1[1] print(list1) #[1,3,4,5,6] ``` ## 【afterclass practice】 1. 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell ) 2. 教學影音 lesson 8