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

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

1、打开JetBrains PyCharm软件。

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

2、在代码编辑区域敲入以下代码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] =items=list(dicts.items()items.sort(key=lambda x:x[1],reverse=True)for item in items: word,count=item print("{:<12}{:>5}".format(word,count))

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统计英文句子中单词出现的次数

4、运行结果如下。

Pyhon统计英文句子中单词出现的次数
  • python素数判断代码
  • python累加求和代码
  • python大小写字母转换
  • python统计字符串中字母数字出现的次数
  • python创建列表
  • 热门搜索
    关于宇宙的手抄报 手抄报a4版面设计图 小学生新年手抄报 三爱手抄报内容 电子手抄报模板 三年级手抄报端午节一等奖 中秋节 手抄报 小学生消防手抄报 法制与安全的手抄报 建军节手抄报内容