统计单词个数

题目

统计单词个数,输出出现次数最多的那个单词

代码

def CountWords(wordslist):
    counts = {}
    max_words = []
    for word in wordslist:
        counts[word] = counts.get(word,0)+1
    for k, v in counts.items():       # 遍历字典一遍找对应的 key 值
        if v == max(counts.values()): # 利用 max 函数先找到最大 value
              max_words .append(k)
    return   max_words 

wordslist = list(map(str.lower,input("Enter a string:\n").split( )))
words = CountWords(wordslist)
for word in words:
    print(word)

输入

this is a python and Python

输出

python

posted @ 2020-05-05 11:13  sinlearn  阅读(401)  评论(0编辑  收藏  举报