0

Python リスト内のアイテムのモードと頻度をどのように見つけますか?

これは私が持っているものです:

elif user_option == 7:
    for score in scores_list:
        count = scores_list.count(score)
        print ("mode(s), occuring" + str(count) + ":")
        print(score)

私がする必要があるのは、ユーザーが 2 つのスコアが同時に表示される一連のスコアを入力した場合に、最も多く表示されるスコアを出力することです。また、実際のスコアも表示する必要があります。しかし、これは私がテストしたときに得られるものです:

Select an option above:7
mode(s), occuring2:
45.0
mode(s), occuring2:
45.0
mode(s), occuring1:
67.0
4

2 に答える 2

2

リストのアイテムの頻度を数えようとしている場合は、これを試してください:

from collections import Counter
data = Counter(your_list_in_here)
data.most_common()   # Returns all unique items and their counts
data.most_common(1)  # Returns the highest occurring item
于 2013-09-29T23:46:58.107 に答える