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 相关文章推荐
pymssql数据库操作MSSQL2005实例分析
May 25 Python
python中zip()方法应用实例分析
Apr 16 Python
Python中Iterator迭代器的使用杂谈
Jun 20 Python
Python虚拟环境virtualenv的安装与使用详解
May 28 Python
Python实现扣除个人税后的工资计算器示例
Mar 26 Python
获取python的list中含有重复值的index方法
Jun 27 Python
python中通过selenium简单操作及元素定位知识点总结
Sep 10 Python
Python调用graphviz绘制结构化图形网络示例
Nov 22 Python
Pycharm中Python环境配置常见问题解析
Jan 16 Python
python实现串口通信的示例代码
Feb 10 Python
Python 如何安装Selenium
May 06 Python
python脚本框架webpy的url映射详解
Nov 20 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
网页游戏开发入门教程二(游戏模式+系统)
2009/11/02 PHP
举例讲解PHP面对对象编程的多态
2015/08/12 PHP
PHP7 echo和print语句实例用法
2019/02/15 PHP
JavaScript DOM 学习第五章 表单简介
2010/02/19 Javascript
基于jQuery的左右滚动实现代码
2010/12/03 Javascript
nodejs命令行参数处理模块commander使用实例
2014/09/17 NodeJs
JSONP和批量操作功能的实现方法
2016/08/21 Javascript
javascript 正则表达式去空行方法
2017/01/24 Javascript
详解AngularJs ui-router 路由的简单介绍
2017/04/26 Javascript
微信小程序开发之animation循环动画实现的让云朵飘效果
2017/07/14 Javascript
nodejs+express搭建多人聊天室步骤
2018/02/12 NodeJs
jQuery实现ajax回调函数带入参数的方法示例
2018/06/26 jQuery
一步快速解决微信小程序中textarea层级太高遮挡其他组件
2019/03/04 Javascript
如何优雅地在vue中添加权限控制示例详解
2019/03/07 Javascript
react 中父组件与子组件双向绑定问题
2019/05/20 Javascript
javascript定时器的简单应用示例【控制方块移动】
2019/06/17 Javascript
layui 动态设置checbox 选中状态的例子
2019/09/02 Javascript
python调用机器喇叭发出蜂鸣声(Beep)的方法
2015/03/23 Python
Python批量重命名同一文件夹下文件的方法
2015/05/25 Python
Python编程产生非均匀随机数的几种方法代码分享
2017/12/13 Python
python爬虫中get和post方法介绍以及cookie作用
2018/02/08 Python
Python全栈之列表数据类型详解
2019/10/01 Python
Python字节单位转换实例
2019/12/05 Python
Pytorch evaluation每次运行结果不同的解决
2020/01/02 Python
Tensorflow tf.dynamic_partition矩阵拆分示例(Python3)
2020/02/07 Python
Python3内置函数chr和ord实现进制转换
2020/06/05 Python
python线性插值解析
2020/07/05 Python
python 多线程共享全局变量的优劣
2020/09/24 Python
canvas实现图片镜像翻转的2种方式
2020/07/22 HTML / CSS
荣耀俄罗斯官网:HONOR俄罗斯
2020/10/31 全球购物
浅谈react路由传参的几种方式
2021/03/23 Javascript
大学毕业的自我鉴定
2013/10/08 职场文书
大学生奖学金获奖感言(范文)
2019/08/15 职场文书
Mac M1安装mnmp (Mac+Nginx+MySQL+PHP) 开发环境
2021/03/29 PHP
node.js如何自定义实现一个EventEmitter
2021/07/16 Javascript
win10壁纸在哪个文件夹 win10桌面背景图片文件位置分享
2022/08/05 数码科技