python——用字典来统计词频
用字典来统计词频
类型:字典
描述
使用freqDict = eval(input())
读入单词词频字典,再读入一段英文,默认按照英文输入的顺序,统计更新单词词频字典,并输出。
输入格式:
输入为两行,第一行是一个字典,形如{'hello': 12, 'world': 10}
,其中存储初始的词频数据。第二行是一段英文文本。
输出格式:
输出一行,直接打印输出更新后的字典。
示例 1
输入:
{}
hello world
输出:
{'hello': 1, 'world': 1}
示例 2
输入:
{'and':20,'in':10}
Anna and Elsa
输出:
{'and': 21, 'in': 10, 'Anna': 1, 'Elsa': 1}
参考代码
freqDict = eval(input())
for word in input().split():
freqDict[word] = freqDict.get(word,0) + 1
print(freqDict)
作者:m0_62488776