単語のリストに最も多く出現した単語の出現回数を返す関数を書いています。
def max_frequency(words):
"""Returns the number of times appeared of the word that
appeared the most in a list of words."""
words_set = set(words)
words_list = words
word_dict = {}
for i in words_set:
count = []
for j in words_list:
if i == j:
count.append(1)
word_dict[i] = len(count)
result_num = 0
for _, value in word_dict.items():
if value > result_num:
result_num = value
return result_num
例えば:
words = ["Happy", "Happy", "Happy", "Duck", "Duck"]
answer = max_frequency(words)
print(answer)
3
ただし、リスト内の大量の単語を処理する場合、この関数は遅くなります。たとえば、250,000 単語のリストでは、この関数が出力を表示するのに 4 分以上かかります。だから私はこれを微調整するために助けを求めています。
何も輸入したくありません。