Python读取英文文件并记录每个单词出现次数后降序输出示例


Posted in Python onJune 28, 2018

本文实例讲述了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)]

Python 相关文章推荐
在Python的Bottle框架中使用微信API的示例
Apr 23 Python
Python+Wordpress制作小说站
Apr 14 Python
python实现比较文件内容异同
Jun 22 Python
Django中日期处理注意事项与自定义时间格式转换详解
Aug 06 Python
Python实现的爬取百度贴吧图片功能完整示例
May 10 Python
pyqt5 使用label控件实时显示时间的实例
Jun 14 Python
使用python接入微信聊天机器人
Mar 31 Python
django-allauth入门学习和使用详解
Jul 03 Python
Flask框架模板继承实现方法分析
Jul 31 Python
python操作excel让工作自动化
Aug 09 Python
django-利用session机制实现唯一登录的例子
Mar 16 Python
学会迭代器设计模式,帮你大幅提升python性能
Jan 03 Python
将Dataframe数据转化为ndarry数据的方法
Jun 28 #Python
Python格式化日期时间操作示例
Jun 28 #Python
Python subprocess模块功能与常见用法实例详解
Jun 28 #Python
对python中array.sum(axis=?)的用法介绍
Jun 28 #Python
Python3连接SQLServer、Oracle、MySql的方法
Jun 28 #Python
对Python中数组的几种使用方法总结
Jun 28 #Python
Python动态导入模块的方法实例分析
Jun 28 #Python
You might like
php运行出现Call to undefined function curl_init()的解决方法
2010/11/02 PHP
PHP中error_reporting()函数的用法(修改PHP屏蔽错误)
2011/07/01 PHP
thinkphp实现like模糊查询实例
2014/10/29 PHP
php实现转换html格式为文本格式的方法
2016/05/16 PHP
Laravel 5.3 学习笔记之 配置
2016/08/28 PHP
phpMyAdmin无法登陆的解决方法
2017/04/27 PHP
centos7上编译安装php7以php-fpm方式连接apache
2018/11/08 PHP
jquery用offset()方法获得元素的xy坐标
2014/09/06 Javascript
js使用post 方式打开新窗口
2015/02/26 Javascript
JavaScript模拟鼠标右键菜单效果
2020/12/08 Javascript
Bootstrap实现弹性搜索框
2016/07/11 Javascript
JS移动端/H5同时选择多张图片上传并使用canvas压缩图片
2017/06/20 Javascript
Ionic3实现图片瀑布流布局
2017/08/09 Javascript
实现div内部滚动条滚动到底部和顶部的代码
2017/11/15 Javascript
最适应的vue.js的form提交涉及多种插件【推荐】
2018/08/27 Javascript
express 项目分层实践详解
2018/12/10 Javascript
ES6入门教程之Array.from()方法
2019/03/23 Javascript
vue-cli设置css不生效的解决方法
2020/02/07 Javascript
JS实现选项卡插件的两种写法(jQuery和class)
2020/12/30 jQuery
VUE实现吸底按钮
2021/03/04 Vue.js
[01:55]2014DOTA2国际邀请赛快报:国土生病 紧急去医院治疗
2014/07/10 DOTA
Python性能优化的20条建议
2014/10/25 Python
Python中实现常量(Const)功能
2015/01/28 Python
浅谈Python数据类型之间的转换
2016/06/08 Python
Python 字典与字符串的互转实例
2017/01/13 Python
Python实现将sqlite数据库导出转成Excel(xls)表的方法
2017/07/17 Python
对python中Json与object转化的方法详解
2018/12/31 Python
python将字符串转变成dict格式的实现
2019/11/18 Python
python 操作hive pyhs2方式
2019/12/21 Python
python 实现屏幕录制示例
2019/12/23 Python
python检查目录文件权限并修改目录文件权限的操作
2020/03/11 Python
Tensorflow中的dropout的使用方法
2020/03/13 Python
python实现拼接图片
2020/03/23 Python
使用Python-OpenCV消除图像中孤立的小区域操作
2020/07/05 Python
Pytorch模型迁移和迁移学习,导入部分模型参数的操作
2021/03/03 Python
Python还能这么玩之用Python做个小游戏的外挂
2021/06/04 Python