Aliz_hkland 2023-05-11 23:43 采纳率: 100%
浏览 30
已结题

python数据分析

从键盘输入一个英文句子或单词组成的字符串,可以不包含标点符号,但单词之间要用空格分开。将句子中单词以及出现的频次分别作为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

  • 写回答

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)
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月20日
  • 已采纳回答 5月12日
  • 创建了问题 5月11日