从键盘输入一个英文句子或单词组成的字符串,可以不包含标点符号,但单词之间要用空格分开。将句子中单词以及出现的频次分别作为key和value保存在字典中,并输出。(提示:字符串的split方法可以将字符串转成列表,列表的count方法可以统计列表元素的频次)
输入样例:
I am Chinese student and I am studying in Henan Agricultural University.
输出样例:
I 2
am 2
Chinese 1
student 1
and 1
studying 1
in 1
Henan 1
Agricultural 1
University 1
python数据分析
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
threenewbee 2023-05-11 23:47关注sentence = input("") words = sentence.replace(".", "").replace(",", "").split() word_count = {} for word in words: if word not in word_count: word_count[word] = 1 else: word_count[word] += 1 for word, count in word_count.items(): print(word, count)本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用