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模拟登录百度代码分享(获取百度贴吧等级)
Dec 27 Python
python解析发往本机的数据包示例 (解析数据包)
Jan 16 Python
python中字典dict常用操作方法实例总结
Apr 04 Python
在Python程序中实现分布式进程的教程
Apr 28 Python
python安装cx_Oracle模块常见问题与解决方法
Feb 21 Python
python3+PyQt5图形项的自定义和交互 python3实现page Designer应用程序
Jul 20 Python
Python实现string字符串连接的方法总结【8种方式】
Jul 06 Python
几行Python代码爬取3000+上市公司的信息
Jan 24 Python
Python3中函数参数传递方式实例详解
May 05 Python
Python GUI编程学习笔记之tkinter控件的介绍及基本使用方法详解
Mar 30 Python
Python数组拼接np.concatenate实现过程
Apr 18 Python
Python devel安装失败问题解决方案
Jun 09 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 自写函数代码 获取关键字 去超链接
2010/02/08 PHP
MySQL连接数超过限制的解决方法
2011/07/17 PHP
关于file_get_contents返回为空或函数不可用的解决方案
2013/06/24 PHP
PHP中的gzcompress、gzdeflate、gzencode函数详解
2014/07/29 PHP
php命名空间设计思想、用法与缺点分析
2019/07/17 PHP
用javascript操作xml
2006/11/04 Javascript
jquery键盘事件使用介绍
2011/11/01 Javascript
可在线编辑网页文字效果代码(单击)
2013/03/02 Javascript
JQuery表格内容过滤的实现方法
2013/07/05 Javascript
JS基于MSClass和setInterval实现ajax定时采集信息并滚动显示的方法
2016/04/18 Javascript
JS实现数字格式千分位相互转换方法
2016/08/01 Javascript
AngularJS中比较两个数组是否相同
2016/08/24 Javascript
详解微信小程序开发之下拉刷新 上拉加载
2016/11/24 Javascript
jquery实现转盘抽奖功能
2017/01/06 Javascript
JS查找数组中重复元素的方法详解
2017/06/14 Javascript
JS非空验证及邮箱验证的实例
2017/08/11 Javascript
Javascript(es2016) import和require用法和区别详解
2017/08/11 Javascript
jQuery图片查看插件Magnify开发详解
2017/12/25 jQuery
Vue-router 中hash模式和history模式的区别
2018/07/24 Javascript
JS数组实现分类统计实例代码
2018/09/30 Javascript
详解vue 项目白屏解决方案
2018/10/31 Javascript
详解bootstrap-fileinput文件上传控件的亲身实践
2019/03/21 Javascript
React中使用外部样式的3种方式(小结)
2019/05/28 Javascript
vue+element 模态框表格形式的可编辑表单实现
2019/06/07 Javascript
JS可断点续传文件上传实现代码解析
2020/07/30 Javascript
利用JS判断元素是否为数组的方法示例
2021/01/08 Javascript
python指定写入文件时的编码格式方法
2018/06/07 Python
Python中的Socket 与 ScoketServer 通信及遇到问题解决方法
2019/04/01 Python
Python对接六大主流数据库(只需三步)
2019/07/31 Python
logging level级别介绍
2020/02/21 Python
细说NumPy数组的四种乘法的使用
2020/12/18 Python
阿迪达斯西班牙官方网站:adidas西班牙
2016/07/21 全球购物
驴妈妈旅游网:中国新型的B2C旅游电子商务网站
2016/08/16 全球购物
小学生感恩父母演讲稿
2014/08/28 职场文书
领导班子个人对照检查剖析材料
2014/09/29 职场文书
幼儿园教师培训心得体会
2016/01/21 职场文书