题目
统计单词个数,输出出现次数最多的那个单词
代码
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:
").split( )))
words = CountWords(wordslist)
for word in words:
print(word)
输入
this is a python and Python
输出
python