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数组复制拷贝的实现方法
Jun 09 Python
详解Python中的Cookie模块使用
Jul 06 Python
Python编程之event对象的用法实例分析
Mar 23 Python
Python 获得13位unix时间戳的方法
Oct 20 Python
使用Python实现微信提醒备忘录功能
Dec 04 Python
解决Python3.5+OpenCV3.2读取图像的问题
Dec 05 Python
学习python分支结构
May 17 Python
python识别图像并提取文字的实现方法
Jun 28 Python
Django-xadmin后台导入json数据及后台显示信息图标和主题更改方式
Mar 11 Python
django 实现手动存储文件到model的FileField
Mar 30 Python
Django REST 异常处理详解
Jul 15 Python
Python爬虫与反爬虫大战
Jul 30 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
《破坏领主》销量已超100万 未来将继续开发新内容
2020/03/08 其他游戏
PHP中if和or运行效率对比
2014/12/12 PHP
php正则替换处理HTML页面的方法
2015/06/17 PHP
ThinkPHP框架整合微信支付之JSAPI模式图文详解
2019/04/09 PHP
JS 建立对象的方法
2007/04/21 Javascript
跟着JQuery API学Jquery 之二 属性
2010/04/09 Javascript
jQuery 选择表格(table)里的行和列及改变简单样式
2012/12/15 Javascript
jQuery中:reset选择器用法实例
2015/01/04 Javascript
javascript将中国数字格式转换成欧式数字格式的简单实例
2016/08/02 Javascript
JS正则表达式修饰符global(/g)用法分析
2016/12/27 Javascript
Jquery实现跨域异步上传文件总结
2017/02/03 Javascript
JS 实现随机验证码功能
2017/02/15 Javascript
vue3.0 CLI - 2.5 - 了解组件的三维
2018/09/14 Javascript
JavaScript 2018 中即将迎来的新功能
2018/09/21 Javascript
解决百度Echarts图表坐标轴越界的方法
2018/10/17 Javascript
如何封装了一个vue移动端下拉加载下一页数据的组件
2019/01/06 Javascript
微信小程序在ios下Echarts图表不能滑动的问题解决
2019/07/10 Javascript
你知道JavaScript Symbol类型怎么用吗
2020/01/08 Javascript
微信小程序webSocket的使用方法
2020/02/20 Javascript
[01:09:01]完美世界DOTA2联赛循环赛 Magma vs PXG BO2第一场 10.28
2020/10/28 DOTA
Python标准异常和异常处理详解
2015/02/02 Python
Python遍历pandas数据方法总结
2018/02/09 Python
python实现逆序输出一个数字的示例讲解
2018/06/25 Python
python 实现将字典dict、列表list中的中文正常显示方法
2018/07/06 Python
解决python3 pika之连接断开的问题
2018/12/18 Python
Tensorflow 实现分批量读取数据
2020/01/04 Python
唤醒头发毛囊的秘密武器:Grow Gorgeous
2016/08/28 全球购物
StubHub墨西哥:购买和出售您的门票
2016/09/17 全球购物
约瑟夫·特纳男装:Joseph Turner
2017/10/10 全球购物
德国消费电子产品购物网站:Guter Kauf
2020/09/15 全球购物
募捐倡议书
2014/04/14 职场文书
行政文员实习自我鉴定范文
2014/09/14 职场文书
外贸业务员岗位职责
2015/02/13 职场文书
专项资金申请报告
2015/05/15 职场文书
Python虚拟环境virtualenv是如何使用的
2021/06/20 Python
MySQL普通表如何转换成分区表
2022/05/30 MySQL