0
私は全く新しいPythonです。私はすでにかなり小さな仕事に苦しんでいます。結婚していると出現順とアルファベット順にリストを並べる
私は(重複した)単語を数え、先頭の "n"語を表示したいし、同輩の場合はアルファベット順に並べ替える単語リストを持っています。
def count_words(s, n):
# TODO: Count the number of occurences of each word in s
wordlist = s.split()
#Count words
counts = Counter(wordlist)
#Get top n words
top_n = counts.most_common(n)
return top_n
しかしprint count_words("cat bat mat cat cat mat mat mat bat bat cat", 3)
のために、それは私に[('mat', 4), ('cat', 4), ('bat', 3)]
を与えますが、私はそれを感謝[('cat', 4), ('mat', 4), ('bat', 3)]
任意のヒントする必要があります:私はこれを書いた最初の2つの部分については
!
*アルファベット順に*、*は(['cat'、4)、( 'mat'、4)、( 'bat'、3)] '*は一致しません。あなたは ''(バット、3)、( 'cat'、4)、( 'mat'、4)] 'を意味しましたか? – RomanPerekhrest
いいえ、結ばれている場合のみです。バットは3回の出現しかないので、最後は来る。 – PrimuS