# wk07_1019_串列與元組_B1003205 醫放三 謝彤 ## [inclass practice] 1. list 2. tuple ----- ### {綜合演練} 實作2 輸入喜歡的水果,直到Enter鍵結束,找尋fruit = ["香蕉","蘋果","橘子","鳳梨","西瓜"]水果串列是否包含此水果,並顯示該水果是串列中的第幾項 ```python list1=[] list2=["草莓","蘋果","葡萄"] list3=[["英國","日本","法國"],["羽球","桌球","跑步"]] print(list1) print(list2) print(list3) ``` [] ['草莓', '蘋果', '葡萄'] [['英國', '日本', '法國'], ['羽球', '桌球', '跑步']] ```python print("我最想要去的國家是%s"%(list3[0][1])) #記得0是第一項 ``` 我最想要去的國家是日本 ```python list2.append("橘子") #.append即新增元素進list print(list2) ``` ['草莓', '蘋果', '葡萄', '橘子', '橘子'] ```python list2.pop() #pop可不寫參數,會刪掉list最後一個元素 ``` '橘子' ```python list2.pop(0) #也可寫參數,會刪掉指定參數 ``` '草莓' ```python print(list2) #被我刪光了(按太多次) ``` [] ```python list2=["草莓","蘋果","葡萄","橘子"] ``` ```python list2[1] = "哈密瓜" print(list2) ``` ['草莓', '哈密瓜', '葡萄', '橘子'] ```python list2.insert(0,"櫻桃") #.insert即插入元素(前面數字可指定位置) print(list2) ``` ['櫻桃', '櫻桃', '草莓', '哈密瓜', '葡萄', '橘子'] ```python list2.insert("火龍果") #若不指定位置,不能插入! print(list2) ``` --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[26], line 1 ----> 1 list2.insert("火龍果") #若不指定位置,不能插入! 2 print(list2) TypeError: insert expected 2 arguments, got 1 ```python list2.remove("哈密瓜") print(list2) ``` ['櫻桃', '櫻桃', '草莓', '葡萄', '橘子'] ```python list4=[100,89,79] print(max(list4)) #列出list的最大值 print(min(list4)) #列出list的最小值 print(len(list4)) #列出list有幾個元素 print(list4.count(79)) #列出list有幾個79 print(list4.index(79)) #列出第一次出現79是在第幾個元素 ``` 100 79 3 1 2 ```python #使用for迴圈讀取串列元素 print(list4) for n in list4: print(n*10,end=' ') #end=' '代表每個輸出之間都有空格 ``` [100, 89, 79] 1000 890 790 ```python for i in range(len(list4)): #(len(list4)表示list有幾個元素=3 print(i) ``` 0 1 2 ```python for i in range(len(list4)): print(i,list4[i],sep=" , ") ``` 0 , 100 1 , 89 2 , 79 ```python #以串列計算班級成績 (要用到append) scores=[100,90,50,70,100,100,90,90,60,30] total=0 avg=0 n=0 for score in scores: total=total+score n+=1 avg=total/n print("班級總成績為%d , 平均成績為%5.2f"%(total,avg)) ``` 班級總成績為780 , 平均成績為78.00 ```python scores=[] score=0 total=0 n=0 while score != -1 : #如果輸入者輸入-1,就會跳出while loop score=int(input("請輸入成績")) if score !=-1: scores.append(score) total=total+score n+=1 avg=total/n print("全班人數有%d人 , 班級總分為%d , 全班平均是%f" %(n,total,avg)) ``` 請輸入成績100 請輸入成績100 請輸入成績100 請輸入成績-1 全班人數有3人 , 班級總分為300 , 全班平均是100.000000 ```python #刪除指定串列元素 fruits=["櫻桃", "櫻桃", "草莓", "哈密瓜", "葡萄", "橘子","香蕉","水梨","番茄"] delete=input("請輸入要刪除的水果") #1019課程終止處 print(fruits) ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ```python ``` ## [afterclass practice] ### 綜合演練 <pre> 1.執行下列程式,下列結果何者正確? (A)0 (B)1 (C)2 (D)[1,2,3,4,5] Answer:(B) ```python list1=[1,2,3,4,5] print(list1[0]) ``` 1 <pre> 2.執行下列程式,下列結果何者正確? (A)香蕉 (B)蘋果 (C)橘子 (D)錯誤,索引值超過範圍 Answer:(A) ```python list4=["香蕉","蘋果","橘子"] print(list4[-3]) ``` 香蕉 <pre> 3.執行下列程式,n的值為多少? (A)0 (B)1 (C)2 (D)3 Answer:(D) ```python scores=[85,79,93] n=len(scores) print(n) ``` 3 <pre> 4.執行下列程式,下列結果何者正確? (A)0 (B)1 (C)2 (D)3 Answer:(B) ```python list1=["香蕉","蘋果","橘子"] n=list1.index("蘋果") print(n) ``` 1 <pre> 5.執行下列程式,下列結果何者正確? (A)n=O (B)n=1 (C)n=2 (D)出現錯誤 Answer:(A) ```python list1=["香蕉","蘋果","橘子"] n = list1.count("西瓜") print(n) ``` 0 <pre> 6.執行下列程式,下列結果何者正確? (A)m=1,n=6 (B)m=2,n=3 (C)m=6,n=2 (D)m=6,n=3 Answer:(D) ```python list1=[1,2,3,4,5,6] m=list1.pop() n=list1.pop(2) print("m=",m,",n=",n) ``` m= 6 ,n= 3 <pre> 7.執行下列程式,下列顯示結果何者正確? (A)-1 (B)12 (C)愛 (D)台灣 Answer:(C) ```python list1=[1,2,3,4,5,6] list1.insert(-1,"愛") #12345愛6 list1.insert(12,"台灣") #12345愛台灣6 print(list1[5]) ``` 愛 <pre> 8.執行下列程式,下列顯示結果何者正確? (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] Answer:(C) ```python list1=[1,2,3,4,5,6] del list1[1] print(list1) ``` [1, 3, 4, 5, 6] <pre> 9.執行下列程式,下列顯示結果何者正確? (A)[3,2,1,5] (B)[1,2,3,5] (C)[5,3,2,1] (D)[5,1,2,3] Answer:(D) ```python list1=[3,2,1,5] list1.reverse() print(list1) ``` [5, 1, 2, 3] <pre> 10.執行下列程式,下列顯示結果何者正確? (A)[3,2,1,5] (B)[1,2,3,5] (C)[5,3,2,1] (D)[5,1,2,3] Answer:(C) ```python list=[3,2,1,5] list2=sorted(list1,reverse=True) print(list2) ``` [5, 3, 2, 1] ## [self practice] <pre> 串列:list,可改變(刪除修改)、有順序、以索引為鍵。可提供儲存資料的記憶體空間,資料型態可以不同也可以相同(可以同時有字串或數字、英文),有一維串列,也有多維串列 元組:tuple,跟串列長很像,但裡面內容不可變 都用index來指定元素 <pre> 若想插入元素,可以使用append和insert append會自動加到最後一項;insert可指定插到哪個位置 想刪除元素可以使用pop和remove