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脚本
Apr 05 Python
Python二叉搜索树与双向链表转换实现方法
Apr 29 Python
python获取指定时间差的时间实例详解
Apr 11 Python
解决Python安装后pip不能用的问题
Jun 12 Python
windows7 32、64位下python爬虫框架scrapy环境的搭建方法
Nov 29 Python
修改python plot折线图的坐标轴刻度方法
Dec 13 Python
numpy中的ndarray方法和属性详解
May 27 Python
Python数据可视化 pyecharts实现各种统计图表过程详解
Aug 15 Python
pandas使用函数批量处理数据(map、apply、applymap)
Nov 27 Python
pyqt5实现井字棋的示例代码
Dec 07 Python
Opencv+Python识别PCB板图片的步骤
Jan 07 Python
python爬虫scrapy基于CrawlSpider类的全站数据爬取示例解析
Feb 20 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
Home Coffee Roasting
2021/03/03 咖啡文化
关于在php.ini中添加extension=php_mysqli.dll指令的说明
2007/06/14 PHP
深入解析PHP的Laravel框架中的event事件操作
2016/03/21 PHP
利用php的ob缓存机制实现页面静态化方法
2017/07/09 PHP
ASP中用Join和Array,可以加快字符连接速度的代码
2007/08/22 Javascript
JS解析XML的实现代码
2009/11/12 Javascript
js实现数字每三位加逗号的方法
2015/02/05 Javascript
详解JavaScript对Date对象的操作问题(生成一个倒数7天的数组)
2015/10/01 Javascript
JavaScript jquery及AJAX小结
2016/01/24 Javascript
JavaScript中将数组进行合并的基本方法讲解
2016/03/07 Javascript
HTML中setCapture、releaseCapture 使用方法浅析
2016/09/25 Javascript
简单学习vue指令directive
2016/11/03 Javascript
angularjs 实现带查找筛选功能的select下拉框实例
2017/01/11 Javascript
js实现简单数字变动效果
2017/11/06 Javascript
学习使用ExpressJS 4.0中的新Router的用法
2018/11/06 Javascript
使用NestJS开发Node.js应用的方法
2018/12/03 Javascript
jquery+css实现Tab栏切换的代码实例
2019/05/14 jQuery
Vue表单控件数据绑定方法详解
2020/02/05 Javascript
python友情链接检查方法
2015/07/08 Python
Python实现针对含中文字符串的截取功能示例
2017/09/22 Python
基于Django框架的权限组件rbac实例讲解
2019/08/31 Python
python文件操作的简单方法总结
2019/11/07 Python
使用IDLE的Python shell窗口实例详解
2019/11/19 Python
python3实现从kafka获取数据,并解析为json格式,写入到mysql中
2019/12/23 Python
Pytorch之卷积层的使用详解
2019/12/31 Python
python GUI库图形界面开发之PyQt5菜单栏控件QMenuBar的详细使用方法与实例
2020/02/28 Python
关于pycharm 切换 python3.9 报错 ‘HTMLParser‘ object has no attribute ‘unescape‘ 的问题
2020/11/24 Python
移动端解决悬浮层(悬浮header、footer)会遮挡住内容的3种方法
2015/03/27 HTML / CSS
Eastbay官网:美国最大的运动鞋网络零售商
2016/07/27 全球购物
日本非常有名的内衣丝袜品牌:GUNZE
2017/01/06 全球购物
C#面试题
2016/05/06 面试题
求职简历的自我评价怎样写好
2013/10/07 职场文书
文明村镇申报材料
2014/05/06 职场文书
党支部书记岗位职责
2015/02/15 职场文书
2015年学生资助工作总结
2015/05/25 职场文书
优秀家长事迹材料(2016推荐版)
2016/02/29 职场文书