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

 时间:2026-02-13 16:56:29

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创建列表
  • 热门搜索
    上海九龙男子医院怎么样 应出勤天数怎么算 赏析怎么写 怎么抽烟教程 老是睡不着怎么办 鱼汤怎么熬 怎么种大蒜 美味的用英文怎么说 lol怎么录制视频 淘宝店铺怎么改名