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之永远强大的函数
Sep 14 Python
在Python中使用正则表达式的方法
Aug 13 Python
Python数据操作方法封装类实例
Jun 23 Python
查看django执行的sql语句及消耗时间的两种方法
May 29 Python
python实现大文本文件分割
Jul 22 Python
python实现坦克大战
Apr 24 Python
Python轻量级web框架bottle使用方法解析
Jun 13 Python
Python调用C/C++的方法解析
Aug 05 Python
python判断一个变量是否已经设置的方法
Aug 13 Python
通过实例解析python and和or使用方法
Nov 14 Python
python实现简单的学生管理系统
Feb 22 Python
Python图片检索之以图搜图
May 31 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
网页里控制图片大小的相关代码
2006/06/13 Javascript
jquery选择器排除某个DOM元素的方法(实例演示)
2014/04/25 Javascript
javascript时间戳和日期字符串相互转换代码(超简单)
2016/06/22 Javascript
angular源码学习第一篇 setupModuleLoader方法
2016/10/20 Javascript
angular.js 路由及页面传参示例
2017/02/24 Javascript
nodejs Assert中equal(),strictEqual(),deepEqual(),strictDeepEqual()比较
2017/09/18 NodeJs
基于Vue2.0+ElementUI实现表格翻页功能
2017/10/23 Javascript
除Console.log()外更多的Javascript调试命令
2018/01/24 Javascript
jquery 获取索引值在一定范围的列表方法
2018/01/25 jQuery
JS实现常见的查找、排序、去重算法示例
2018/05/21 Javascript
小程序调用微信支付的方法
2019/09/26 Javascript
Vue实现base64编码图片间的切换功能
2019/12/04 Javascript
python计算圆周率pi的方法
2015/07/11 Python
Python for Informatics 第11章 正则表达式(一)
2016/04/21 Python
PyCharm配置mongo插件的方法
2018/11/30 Python
python 实现将txt文件多行合并为一行并将中间的空格去掉方法
2018/12/20 Python
利用python计算时间差(返回天数)
2019/09/07 Python
Python matplotlib绘制饼状图功能示例
2019/09/10 Python
Python实现栈和队列的简单操作方法示例
2019/11/29 Python
python GUI库图形界面开发之PyQt5 MDI(多文档窗口)QMidArea详细使用方法与实例
2020/03/05 Python
python根据字典的键来删除元素的方法
2020/08/16 Python
python中time tzset()函数实例用法
2021/02/18 Python
HTML5如何为形状图上颜色怎么绘制具有颜色和透明度的矩形
2014/06/23 HTML / CSS
鲜果饮品店创业计划书
2014/01/21 职场文书
实验教师岗位职责
2014/02/13 职场文书
金融管理专业毕业生求职信
2014/03/12 职场文书
欢度春节标语
2014/07/01 职场文书
2014卖家双十一活动策划书
2014/09/29 职场文书
交通事故协议书范文
2014/10/23 职场文书
大学生个人年度总结范文
2015/02/15 职场文书
党员自我评价2015
2015/03/03 职场文书
求职简历自我评价范文
2015/03/10 职场文书
乡镇团委工作总结2015
2015/05/26 职场文书
男人帮观后感
2015/06/18 职场文书
小学远程教育工作总结
2015/08/13 职场文书
利用python进行数据加载
2021/06/20 Python