网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

Pyhon统计英文句子中单词出现的次数

时间:2024-10-13 06:03:33

1、打开JetBrains PyCharm软件。

Pyhon统计英文句子中单词出现的次数

3、本代码的注释如下:sentence=input("请输入需要统计的英文句子:")#将文本中涉及的标点用空格替代for ch in ",.?!": sentence=sentence.replace(ch," ")#利用字典统计词频words=sentence.split()dicts={}for word in words: if word in dicts: dicts[word]+=1 else: dicts[word] = 1#对统计结果排序items=list(dicts.items())#对单词出现的次数reverse=True降序items.sort(key=lambda x:x[1],reverse=True)for item in items: word,count=item print("{:<12}{:>5}".format(word,count))

Pyhon统计英文句子中单词出现的次数
© 2025 阿力知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com