• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python找出文件中使用率最高的汉字实例详解

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python 汉字编码,python 汉字,python 汉字转拼音,python输出汉字,python 汉字乱码等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

</div>

如果想用上新方法,以及让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) 
</div>

或者让行数更少(好囧的列表综合):

# -*- 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) 
</div>

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

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

</div>

引入psyco模块的成绩:

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

 

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

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

</div>

您可能想查找下面的文章:

  • python 遍历字符串(含汉字)实例详解
  • python 获取网页编码方式实现代码
  • Python 包含汉字的文件读写之每行末尾加上特定字符
  • python中的编码知识整理汇总
  • Python找出文件中使用率最高的汉字实例详解

相关文章

  • 零基础学Python(一)Python环境安装
  • Python计算字符宽度的方法
  • python通过装饰器检查函数参数数据类型的方法
  • python实现根据ip地址反向查找主机名称的方法
  • Python实现自动添加脚本头信息的示例代码
  • 在Python中使用swapCase()方法转换大小写的教程
  • python实现的DES加密算法和3DES加密算法实例
  • 用Python实现一个简单的线程池
  • python实现爬取千万淘宝商品的方法
  • python使用clear方法清除字典内全部数据实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 使用 tf.nn.dynamic_rnn 展开时间维度方式
    • python协程用法实例分析
    • python基础教程之分支、循环简单用法
    • Python异常处理总结
    • 在Python中使用NLTK库实现对词干的提取的教程
    • 使用Python的urllib2模块处理url和图片的技巧两则
    • python显示天气预报
    • pydev使用wxpython找不到路径的解决方法
    • Python实现删除文件但保留指定文件
    • 解决python3 urllib中urlopen报错的问题

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有