2022年9月16日 星期五

Python b374: [福州19中]众数

b374: [福州19中]众数


from collections import Counter


# 出现频率最高的3个单词



print('b374: [福州19中]眾數')

while True:

    try:

        n = int(input('正整数的个数N--->'))   #


        nums = list(map(int,input("\nEnter the numbers (N个正整数) : ").strip().split()))[:n]

        word_counts = Counter(nums)

        top_one = word_counts.most_common(n)

        print('\n眾數 次數')

        for i in range (len(top_one)):

            print(top_one[i])

        print('\n')

    except:

        break 



>>> %Run -c $EDITOR_CONTENT

b374: [福州19中]眾數

正整数的个数N--->12


Enter the numbers (N个正整数) : 2  4  2  3  2  5  3  7  2  3  4  3


眾數 次數

(2, 4)

(3, 4)

(4, 2)

(5, 1)

(7, 1)



正整数的个数N--->

>>> 



# https://steam.oxxostudio.tw/category/python/zerojudge/b374.html

# b374: [福州19中]众数


from collections import Counter    # 載入 collections 函式庫的 Counter


print('b374: [福州19中]眾數')

while True:

  try:

    n = int(input('正整数的个数N--->'))                          # 有幾個數字

    num = [int(i) for i in input('\nEnter the numbers (N个正整数) : ').split()]   # 將輸入的文字轉換成數字串列

    a = Counter(num)      # 計算每個數字出現的次數,轉成字典的型態

    b, c = [], []         # 建立兩個空串列,記錄數字以及排序使用

    for key in a:

      b.append(a[key])    # 將所有的次數存入串列中

    m = max(b)            # 取出數字裡的最大值 ( 眾數的次數 )

    for key in a:

      if a[key] == m:     # 如果該數字的次數等於最大值

        c.append(key)     # 將該數字存入 c 串列

    c.sort()              # 排序

    for i in c:

      print(f'{i} {m}')   # 印出結果

  except:

    break


>>> %Run -c $EDITOR_CONTENT

b374: [福州19中]眾數

正整数的个数N--->12


Enter the numbers (N个正整数) : 2  4  2  3  2  5  3  7  2  3  4  3

2 4

3 4

正整数的个数N--->

>>> 


沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...