Python读取英文文件并记录每个单词出现次数后降序输出示例

yipeiwu_com5年前Python基础

本文实例讲述了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)]

PS:这里再为大家推荐2款相关统计工具供大家参考:

在线字数统计工具:
http://tools.jb51.net/code/zishutongji

在线字符统计与编辑工具:
http://tools.jb51.net/code/char_tongji

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

在Python中利用pickle保存变量的实例

在Python中利用pickle保存变量的实例

在工作中出于某些原因,我们可能需要将变量保存下来,这样下次就可以直接去赋值而不用重新执行某些重复耗时的操作了,这里我们用到了Python的pickle包来做变量的存储和变量加载,大家注意...

python中异常报错处理方法汇总

python中异常报错处理方法汇总

首先异常是什么,异常白话解释就是不正常,程序里面一般是指程序员输入的格式不规范,或者需求的参数类型不对应,不全等等。 Python中异常是指程序中的例外,违例情况。异常机制是指程序出现错...

python实现微信跳一跳辅助工具步骤详解

说明 1.windows上安装安卓模拟器,安卓版本5.1以上 2.模拟器里下载安装最新的微信6.6.1 3.最好使用python2.7,python3的pyhook包有bug,解决比较麻...

Python面向对象思想与应用入门教程【类与对象】

本文实例讲述了Python面向对象思想与应用。分享给大家供大家参考,具体如下: 面向对象思想 1、面向对象的设计思想 面向对象是基于万物皆对象这个哲学观点。 2、面向对象和面向过程...

Python使用add_subplot与subplot画子图操作示例

Python使用add_subplot与subplot画子图操作示例

本文实例讲述了Python使用add_subplot与subplot画子图操作。分享给大家供大家参考,具体如下: 子图:就是在一张figure里面生成多张子图。 Matplotlib对象...