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求crc32值的方法
Oct 05 Python
Python实现截屏的函数
Jul 25 Python
详解Python迭代和迭代器
Mar 28 Python
Python数据类型详解(四)字典:dict
May 12 Python
Python输出由1,2,3,4组成的互不相同且无重复的三位数
Feb 01 Python
python paramiko利用sftp上传目录到远程的实例
Jan 03 Python
Python字符串的常见操作实例小结
Apr 08 Python
基于Python获取城市近7天天气预报
Nov 26 Python
在pycharm中使用matplotlib.pyplot 绘图时报错的解决
Jun 01 Python
python 实现音频叠加的示例
Oct 29 Python
python 实现学生信息管理系统的示例
Nov 28 Python
浅析python中特殊文件和特殊函数
Feb 24 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 VS ASP
2006/10/09 PHP
浅析十款PHP开发框架的对比
2013/07/05 PHP
PHP 伪静态技术原理以及突破原理实现介绍
2013/07/12 PHP
去除php注释和去除空格函数分享
2014/03/13 PHP
php遍历文件夹下的所有文件和子文件夹示例
2014/03/20 PHP
PHP使用PDO、mysqli扩展实现与数据库交互操作详解
2019/07/20 PHP
Laravel 5.2 文档 数据库 ―― 起步介绍
2019/10/21 PHP
浅谈JavaScript中定义变量时有无var声明的区别
2014/08/18 Javascript
jQuery中:radio选择器用法实例
2015/01/03 Javascript
javascript实现鼠标移到Image上方时显示文字效果的方法
2015/08/07 Javascript
基于JS实现回到页面顶部的五种写法(从实现到增强)
2016/09/03 Javascript
Vue 过渡实现轮播图效果
2017/03/27 Javascript
ionic3 懒加载
2017/08/16 Javascript
详解Vue2.0 事件派发与接收
2017/09/05 Javascript
JS中的BOM应用
2018/02/02 Javascript
微信小程序使用gitee进行版本管理
2018/09/20 Javascript
vue移动端实现手机左右滑动入场动画
2020/06/17 Javascript
vue中实现动态生成二维码的方法
2020/02/21 Javascript
原生JS实现留言板
2020/03/26 Javascript
Vue路由的模块自动化与统一加载实现
2020/06/05 Javascript
如何在 Vue 中使用 JSX
2021/02/14 Vue.js
python入门基础之用户输入与模块初认识
2016/11/14 Python
基于Python代码编辑器的选用(详解)
2017/09/13 Python
详解Python 正则表达式模块
2018/11/05 Python
python按修改时间顺序排列文件的实例代码
2019/07/25 Python
Python OpenCV视频截取并保存实现代码
2019/11/30 Python
详解python中groupby函数通俗易懂
2020/05/14 Python
python中有函数重载吗
2020/05/28 Python
爱岗敬业演讲稿范文
2014/01/14 职场文书
幼儿园门卫岗位职责
2014/02/14 职场文书
2014年数学教师工作总结
2014/12/03 职场文书
初中开学典礼新闻稿
2015/07/17 职场文书
严以用权学习心得体会
2016/01/12 职场文书
党员理论学习心得体会
2016/01/21 职场文书
Node.js实现断点续传
2021/06/23 Javascript
Python如何快速找到多个字典中的公共键(key)
2022/04/29 Python