Python统计英文文件词频并降序输出的实现方法

江岸
江岸
发布时间:2026-10-13| 3人浏览过
原创

本文介绍使用Python读取英文文件并统计每个单词出现次数的方法。通过去除标点符号、分割字符串并构建字典计数,最终利用sorted函数按值降序输出结果。文中提供了具体的代码示例、排序参数说明及在线工具推荐,适用于Python初学者进行文本处理练习。

本文分享一个使用Python读取英文文件并统计每个单词出现次数,最后按频率降序输出的完整示例,供读者参考学习。

代码中对文本中出现的句号、逗号和感叹号进行了预处理,将其替换为空格以确保单词分割的准确性。

关于sorted排序函数的具体用法说明如下:

若需按照字典的值(value)进行降序排列,可使用以下代码:

sorted(dict.items(),key=lambda k:k[1],reverse=True)

若需按照字典的值(value)进行升序排列,可使用以下代码:

sorted(dict.items(),key=lambda k:k[1],reverse=False)

或者简写为:

sorted(dict.items(),key=lambda k:k[1])

若需按照字典的键(key)进行降序排列,可使用以下代码:

sorted(dict.items(),key=lambda k:k[0],reverse=True)

若需按照字典的键(key)进行升序排列,可使用以下代码:

sorted(dict.items(),key=lambda k:k[0])

或者简写为:

sorted(dict.items(),key=lambda k:k[0],reverse=False)

以下是完整的Python实现示例:

# -*- coding:utf-8 -*-
#! python2
file_object=open("english.txt")
dict={}
for line in file_object:
  line=line.replace(","," ")
  line=line.replace("."," ")
  line=line.replace("!"," ")
  strs= line.split();
  for str in strs:
    if dict.has_key(str):
      dict[str]+=1
    else:
      dict[str]=1
result=sorted(dict.items(),key=lambda k:k[1],reverse=True)
print result

测试用的英文文件english.txt内容如下:

We are busy all day, like swarms of flies without souls, noisy, restless, unable to hear the voices of the soul. As time goes by, childhood away, we grew up, years away a lot of memories, once have also eroded the bottom of the childish innocence, we regardless of the shackles of mind, indulge in the world buckish, focus on the beneficial principle, we have lost themselves.

程序运行后的输出结果如下:

[('the', 7), ('of', 6), ('we', 3), ('have', 2), ('away', 2), ('flies', 1), ('regardless', 1), ('restless', 1), ('up', 1), ('indulge', 1), ('mind', 1), ('all', 1), ('voices', 1), ('are', 1), ('in', 1), ('We', 1), ('busy', 1), ('shackles', 1), ('also', 1), ('memories', 1), ('by', 1), ('to', 1), ('unable', 1), ('goes', 1), ('themselves', 1), ('lot', 1), ('on', 1), ('buckish', 1), ('focus', 1), ('souls', 1), ('hear', 1), ('innocence', 1), ('world', 1), ('years', 1), ('day', 1), ('noisy', 1), ('a', 1), ('eroded', 1), ('grew', 1), ('like', 1), ('lost', 1), ('swarms', 1), ('bottom', 1), ('soul', 1), ('As', 1), ('without', 1), ('principle', 1), ('beneficial', 1), ('time', 1), ('childish', 1), ('childhood', 1), ('once', 1)]

此外,推荐两款相关的在线统计工具供读者参考:

在线字数统计工具: http://tools.jb51.net/code/zishutongji

在线字符统计与编辑工具: http://tools.jb51.net/code/char_tongji

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述的代码示例和解析对大家的Python程序设计有所帮助。

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。