Python找出文件中使用率最高的汉字实例详解


Posted in Python onJune 03, 2015

本文实例讲述了Python找出文件中使用率最高的汉字的方法。分享给大家供大家参考。具体分析如下:

这是我初学Python时写的,为了简便,我并没在排序完后再去掉非中文字符,稍微会影响性能(大约增加了25%的时间)。

# -*- coding: gbk -*- 
import codecs 
from time import time 
from operator import itemgetter 
def top_words(filename, size=10, encoding='gbk'): 
  count = {} 
  for line in codecs.open(filename, 'r', encoding): 
    for word in line: 
      if u'\u4E00' <= word <= u'\u9FA5' or u'\uF900' <= word <= u'\uFA2D': 
        count[word] = 1 + count.get(word, 0) 
  top_words = sorted(count.iteritems(), key=itemgetter(1), reverse=True)[:size] 
  print '\n'.join([u'%s : %s次' % (word, times) for word, times in top_words]) 
begin = time() 
top_words('空之境界.txt') 
print '一共耗时 : %s秒' % (time()-begin)

如果想用上新方法,以及让join的可读性更高的话,这样也是可以的:

# -*- coding: gbk -*- 
import codecs 
from time import time 
from operator import itemgetter 
from heapq import nlargest 
def top_words(filename, size=10, encoding='gbk'): 
  count = {} 
  for line in codecs.open(filename, 'r', encoding): 
    for word in line: 
      if u'\u4E00' <= word <= u'\u9FA5' or u'\uF900' <= word <= u'\uFA2D': 
        count[word] = 1 + count.get(word, 0) 
  top_words = nlargest(size, count.iteritems(), key=itemgetter(1)) 
  for word, times in top_words: 
    print u'%s : %s次' % (word, times) 
begin = time() 
top_words('空之境界.txt') 
print '一共耗时 : %s秒' % (time()-begin)

或者让行数更少(好?宓牧斜碜酆希??/p>

# -*- coding: gbk -*- 
import codecs 
from time import time 
from operator import itemgetter 
def top_words(filename, size=10, encoding='gbk'): 
  count = {} 
  for word in [word for word in codecs.open(filename, 'r', encoding).read() if u'\u4E00' <= word <= u'\u9FA5' or u'\uF900' <= word <= u'\uFA2D']: 
    count[word] = 1 + count.get(word, 0) 
  top_words = sorted(count.iteritems(), key=itemgetter(1), reverse=True)[:size] 
  print '\n'.join([u'%s : %s次' % (word, times) for word, times in top_words]) 
begin = time() 
top_words('空之境界.txt') 
print '一共耗时 : %s秒' % (time()-begin)

此外还可以引入with语句,这样只需一行就能获得异常安全性。
3者性能几乎一样,结果如下:

的 : 17533次
是 : 8581次
不 : 6375次
我 : 6168次
了 : 5586次
一 : 5197次
这 : 4394次
在 : 4264次
有 : 4188次
人 : 4025次
一共耗时 : 0.5秒

引入psyco模块的成绩:

的 : 17533次
是 : 8581次
不 : 6375次
我 : 6168次
了 : 5586次
一 : 5197次
这 : 4394次
在 : 4264次
有 : 4188次
人 : 4025次
一共耗时 : 0.280999898911秒

 

注:测试文件为778KB的GBK编码,40余万字。

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

Python 相关文章推荐
一篇文章快速了解Python的GIL
Jan 12 Python
Python使用matplotlib绘图无法显示中文问题的解决方法
Mar 14 Python
python list删除元素时要注意的坑点分享
Apr 18 Python
pandas ix &amp;iloc &amp;loc的区别
Jan 10 Python
解析Python的缩进规则的使用
Jan 16 Python
Python pip替换为阿里源的方法步骤
Jul 02 Python
基于python的selenium两种文件上传操作实现详解
Sep 19 Python
Python格式化输出--%s,%d,%f的代码解析
Apr 29 Python
python numpy库np.percentile用法说明
Jun 08 Python
Python中全局变量和局部变量的理解与区别
Feb 07 Python
详解python日志输出使用配置文件格式
Feb 10 Python
解析python中的jsonpath 提取器
Jan 18 Python
Python实现Windows上气泡提醒效果的方法
Jun 03 #Python
Python捕捉和模拟鼠标事件的方法
Jun 03 #Python
Python while、for、生成器、列表推导等语句的执行效率测试
Jun 03 #Python
Python fileinput模块使用实例
Jun 03 #Python
以windows service方式运行Python程序的方法
Jun 03 #Python
自己编程中遇到的Python错误和解决方法汇总整理
Jun 03 #Python
python中list常用操作实例详解
Jun 03 #Python
You might like
PHP实现根据图片色界在不同位置加水印的方法
2015/08/08 PHP
PHP Curl模拟登录微信公众平台、新浪微博实例代码
2016/01/28 PHP
php mysqli查询语句返回值类型实例分析
2016/06/29 PHP
PHP反射基础知识回顾
2020/09/10 PHP
eval的两组性能测试数据
2012/08/17 Javascript
Jquery仿IGoogle实现可拖动窗口示例代码
2014/08/22 Javascript
javascript动态创建链接的方法
2015/05/13 Javascript
CSS3 media queries结合jQuery实现响应式导航
2016/09/30 Javascript
微信小程序 picker 组件详解及简单实例
2017/01/10 Javascript
js时间戳与日期格式之间转换详解
2017/12/11 Javascript
jQuery实现定时隐藏对话框的方法分析
2018/02/12 jQuery
node结合swig渲染摸板的方法
2018/04/11 Javascript
vue中 this.$set的用法详解
2019/09/06 Javascript
微信小程序 组件的外部样式externalClasses使用详解
2019/09/06 Javascript
[01:03:27]Optic vs VGJ.S 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
[51:52]Liquid vs Secret 2019国际邀请赛淘汰赛 败者组 BO3 第二场 8.24
2019/09/10 DOTA
定制FileField中的上传文件名称实例
2017/08/23 Python
对Python中9种生成新对象的方法总结
2018/05/23 Python
详解python项目实战:模拟登陆CSDN
2019/04/04 Python
Django Rest framework权限的详细用法
2019/07/25 Python
python程序 创建多线程过程详解
2019/09/23 Python
python3中numpy函数tile的用法详解
2019/12/04 Python
pycharm新建Vue项目的方法步骤(图文)
2020/03/04 Python
python实现在线翻译
2020/06/18 Python
如何使用python记录室友的抖音在线时间
2020/06/29 Python
python创建文本文件的简单方法
2020/08/30 Python
python用tkinter实现一个简易能进行随机点名的界面
2020/09/27 Python
numba提升python运行速度的实例方法
2021/01/25 Python
Python 转移文件至云对象存储的方法
2021/02/07 Python
解决margin 外边距合并问题
2019/07/03 HTML / CSS
应届大学生求职的自我评价
2013/11/17 职场文书
九年级英语教学反思
2014/01/31 职场文书
党建目标管理责任书
2014/07/25 职场文书
党员学习中共十八大报告思想汇报
2014/09/15 职场文书
培训后的感想
2015/08/07 职场文书
Java设计模式之代理模式
2022/04/22 Java/Android