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计算N天之后日期的方法
Mar 31 Python
在Python中使用poplib模块收取邮件的教程
Apr 29 Python
Python升级导致yum、pip报错的解决方法
Sep 06 Python
Python批量提取PDF文件中文本的脚本
Mar 14 Python
Python应用库大全总结
May 30 Python
Python根据已知邻接矩阵绘制无向图操作示例
Jun 23 Python
django Serializer序列化使用方法详解
Oct 16 Python
浅谈Python中eval的强大与危害
Mar 13 Python
对Python函数设计规范详解
Jul 19 Python
Python vtk读取并显示dicom文件示例
Jan 13 Python
Python+PyQt5实现灭霸响指功能
May 25 Python
python实现语音常用度量方法的代码详解
May 25 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
晋城吧对DiscuzX进行的前端优化要点
2010/09/05 PHP
php多个字符串替换成同一个的解决方法
2013/06/18 PHP
php单例模式的简单实现方法
2016/06/10 PHP
Javascript学习笔记2 函数
2010/01/11 Javascript
JavaScript中的一些定位属性[图解]
2010/07/14 Javascript
可自己添加html的伪弹出框实现代码
2013/09/08 Javascript
JS实现一键回顶功能示例代码
2013/10/28 Javascript
Jquery解析json数据详解
2013/12/26 Javascript
javascript中字符串拼接详解
2014/09/26 Javascript
基于jQuery实现的菜单切换效果
2015/10/16 Javascript
深入理解js数组的sort排序
2016/05/28 Javascript
功能强大的Bootstrap使用手册(一)
2016/08/02 Javascript
js获取腾讯视频ID的方法
2016/10/03 Javascript
javascript基于原型链的继承及call和apply函数用法分析
2016/12/15 Javascript
three.js实现围绕某物体旋转
2017/01/25 Javascript
jquery中$.fn和图片滚动效果实现的必备知识总结
2017/04/21 jQuery
react高阶组件添加和删除props
2019/04/26 Javascript
Vue+Django项目部署详解
2019/05/30 Javascript
详解将微信小程序接口Promise化并使用async函数
2019/08/05 Javascript
微信小程序 下拉刷新及上拉加载原理解析
2019/11/06 Javascript
React学习之JSX与react事件实例分析
2020/01/06 Javascript
Vue中强制组件重新渲染的正确方法
2021/01/03 Vue.js
Python减少循环层次和缩进的技巧分析
2016/03/15 Python
全面了解python中的类,对象,方法,属性
2016/09/11 Python
Python的SimpleHTTPServer模块用处及使用方法简介
2018/01/22 Python
pycharm安装和首次使用教程
2018/08/27 Python
浅谈PyQt5 的帮助文档查找方法,可以查看每个类的方法
2019/06/25 Python
python调试神器PySnooper的使用
2019/07/03 Python
在Python中append以及extend返回None的例子
2019/07/20 Python
详解python中的lambda与sorted函数
2020/09/04 Python
快速实现一个简单的canvas迷宫游戏的示例
2018/07/04 HTML / CSS
微软香港官网及网上商店:Microsoft HK
2016/09/01 全球购物
英国水族馆和池塘用品购物网站:Warehouse Aquatics
2019/08/29 全球购物
详细介绍python类及类的用法
2021/05/31 Python
php实现自动生成验证码的实例讲解
2021/11/17 PHP
redis复制有可能碰到的问题汇总
2022/04/03 Redis