#Python TQC考題604 眾數
#視訊解題的作法太多層,一層又一層
#邏輯也不好理解
L=[]
cnt=[0]*10 #不是[ ]空矩陣
for i in range(10):
n=eval(input())
L.append(n) #非L=L.append(n)
cnt[L.index(n)]+=1
#L.index(n), n若重複出現,
#仍會回傳 第一次出現時的index
#cnt陣列的index同L陣列
“””
18是眾數,出現3次
| L陣列 | 34 | 18 | 22 | 32 | 18 | 29 | 30 | 38 | 42 | 18 |
| L陣列index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| cnt陣列_起始 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| cnt陣列index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 第1次迴圈 | 1 | |||||||||
| 第2次迴圈 | 1 | 1 | ||||||||
| 第3次迴圈 | 1 | 1 | 1 | |||||||
| 第4次迴圈 | 1 | 1 | 1 | 1 | ||||||
| 第5次迴圈 | 1 | 2 | 1 | 1 | 0 | |||||
| 第6次迴圈 | 1 | 2 | 1 | 1 | 0 | 1 | ||||
| 第7次迴圈 | 1 | 2 | 1 | 1 | 0 | 1 | 1 | |||
| 第8次迴圈 | 1 | 2 | 1 | 1 | 0 | 1 | 1 | 1 | ||
| 第9次迴圈 | 1 | 2 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | |
| 第10次迴圈 | 1 | 3 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 0 |
| cnt陣列index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
(空白是初始值0)
經過10次迴圈後,
cnt陣列中最大值為3 (眾數出現3次)
max(cnt)=3
index=1 = L陣列的index
=> L[1]=18 (眾數)
cnt.index(max(cnt))=1
L[cnt.index(max(cnt))]=18
從最內層的max(cnt)往外寫
“””
print(L[cnt.index(max(cnt))])
#眾數,太多層,真的不好寫
print(max(cnt))
#眾數出現次數
Spyder執行結果:
![Python TQC考題604 眾數, cnt[L.index(n)]+=1, L[cnt.index(max(cnt))], if L.count(n)>maxcnt: - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/04/20220430181911_73.png)
#再做一次一樣的邏輯
![Python TQC考題604 眾數, cnt[L.index(n)]+=1, L[cnt.index(max(cnt))], if L.count(n)>maxcnt: - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/04/20220515220646_20.png)
#第二種做法,
#第三次做的更簡潔
list1=[]
maxnum=0
#眾數,題目是18
maxcnt=0
#眾數出現的次數,題目是3次
for i in range(10):
list1.append(eval(input()))
#10個數值存在串列中
for j in range(10):
if list1.count(list1[j])>maxcnt:
#比較串列中元素出現次數的多寡
maxcnt=list1.count(list1[j])
maxnum=list1[j]
#眾數
#每一次迴圈都去計算
#是否為出現最多次的數字 .count()
#若是,紀錄其次數與數值
print(maxnum)
print(maxcnt)
![Python TQC考題604 眾數, cnt[L.index(n)]+=1, L[cnt.index(max(cnt))], if L.count(n)>maxcnt: - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/04/20220502164348_60.png)
“””
跟前面一樣的判斷邏輯
但只用一個for迴圈
一邊把使用者輸入的n
append進去list L
一邊比較L.count(n)
是否大於maxcnt(起始值0)
若是,maxcnt=maxcnt+1; maxnum=n
不需要append用一次迴圈
比較L.count(n)再多用一次迴圈
故意跟題目不一樣,
眾數改為-18,
就算眾數是負數,程式也不會出錯
眾數的起始值0沒問題
“””
maxnum=0
#眾數的英文其實是mode或mass
maxcnt=0
L=[]
for i in range(10):
n=eval(input())
L.append(n)
if L.count(n)>maxcnt:
#本題的關鍵就在這個判斷邏輯
maxcnt=maxcnt+1;
maxnum=n
print(maxnum)
print(maxcnt)
![Python TQC考題604 眾數, cnt[L.index(n)]+=1, L[cnt.index(max(cnt))], if L.count(n)>maxcnt: - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/04/20220505074726_45.png)
“””
故意把題目的18改為-18
眾數mode馬上就被n蓋掉
起始值是多少,其實沒關係
即使眾數=0,跟起始值一樣
也沒有出錯
“””
![Python TQC考題604 眾數, cnt[L.index(n)]+=1, L[cnt.index(max(cnt))], if L.count(n)>maxcnt: - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/04/20220515222905_39.png)
#mode眾數:
![Python TQC考題604 眾數, cnt[L.index(n)]+=1, L[cnt.index(max(cnt))], if L.count(n)>maxcnt: - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/04/20220521161025_67.png)
#重點還是這一行 if L.count(n) > maxcnt:
# maxcnt = L.count(n) ,這樣寫也可以
![Python TQC考題604 眾數, cnt[L.index(n)]+=1, L[cnt.index(max(cnt))], if L.count(n)>maxcnt: - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2022/04/20220522150301_35.jpg)


![Python: 如何使用 os.environ[“PATH”] 設定環境變數?與 sys.path.append() 差別為何? Python: 如何使用 os.environ[“PATH”] 設定環境變數?與 sys.path.append() 差別為何?](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2024/09/20240905135312_0_890fa1.png?quality=90&zoom=2&ssl=1&resize=350%2C233)







近期留言