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使用fileinput模块实现逐行读取文件的方法
Apr 29 Python
Linux下将Python的Django项目部署到Apache服务器
Dec 24 Python
Python 搭建Web站点之Web服务器与Web框架
Nov 06 Python
浅谈python内置变量-reversed(seq)
Jun 21 Python
python实现推箱子游戏
Mar 25 Python
Python人工智能之路 jieba gensim 最好别分家之最简单的相似度实现
Aug 13 Python
基于Python的微信机器人开发 微信登录和获取好友列表实现解析
Aug 21 Python
Python实现生成密码字典的方法示例
Sep 02 Python
python中的RSA加密与解密实例解析
Nov 18 Python
Python实现SMTP邮件发送
Jun 16 Python
Python实现文本文件拆分写入到多个文本文件的方法
Apr 18 Python
python程序的组织结构详解
Dec 06 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
ThinkPHP5&5.1实现验证码的生成、使用及点击刷新功能示例
2020/02/07 PHP
PHP标准库 (SPL)――Countable用法示例
2020/06/05 PHP
List the Stored Procedures in a SQL Server database
2007/06/20 Javascript
js实现按钮加背景图片常用方法
2014/11/01 Javascript
jQuery中:hidden选择器用法实例
2014/12/30 Javascript
基于JS实现PHP的sprintf函数实例
2015/11/14 Javascript
Node.js 日志处理模块log4js
2016/08/28 Javascript
微信小程序  action-sheet详解及实例代码
2016/11/09 Javascript
深入浅析AngularJS中的一次性数据绑定 (bindonce)
2017/05/11 Javascript
Vue.js简易安装和快速入门(第二课)
2017/10/17 Javascript
Node.js 利用cheerio制作简单的网页爬虫示例
2018/03/01 Javascript
详解Vue打包优化之code spliting
2018/04/09 Javascript
如何封装了一个vue移动端下拉加载下一页数据的组件
2019/01/06 Javascript
微信小程序访问豆瓣电影api的实现方法
2019/03/31 Javascript
微信小程序授权登录解决方案的代码实例(含未通过授权解决方案)
2019/05/10 Javascript
PHP 502bad gateway原因及解决方案
2020/11/13 Javascript
JavaScript实现网页下拉菜单效果
2020/11/20 Javascript
Python中实现的RC4算法
2015/02/14 Python
Python学习之用pygal画世界地图实例
2017/12/07 Python
Python登录并获取CSDN博客所有文章列表代码实例
2017/12/28 Python
Python使用扩展库pywin32实现批量文档打印实例
2020/04/09 Python
Python并发爬虫常用实现方法解析
2020/11/19 Python
css3中检验表单的required,focus,valid和invalid样式
2014/02/21 HTML / CSS
12个不为大家熟知的HTML5设计小技巧
2016/06/02 HTML / CSS
美国隐形眼镜网上商店:Lens.com
2019/09/03 全球购物
美国排名第一的葡萄酒俱乐部:Firstleaf Wine Club
2020/01/02 全球购物
MIKI HOUSE美国官方网上商店:日本领先的婴儿和儿童高级时装品牌
2020/06/21 全球购物
软件测试工程师笔试题带答案
2015/03/27 面试题
告诉你怎样写创业计划书
2014/01/27 职场文书
2014年机关植树节活动方案
2014/02/27 职场文书
办公室员工岗位工作职责
2014/03/10 职场文书
运动会方队口号
2014/06/07 职场文书
跑操口号
2014/06/12 职场文书
网络技术专业求职信
2014/07/13 职场文书
办理护照工作证明
2014/10/10 职场文书
主婚人致辞精选
2015/07/28 职场文书