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

作者:xm1331305 时间:2021-11-06 06:37:53 

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

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


# -*- 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,文件,汉字
0
投稿

猜你喜欢

  • python turtle绘图命令及案例

    2022-04-29 10:26:58
  • python中lambda函数 list comprehension 和 zip函数使用指南

    2021-08-28 22:16:21
  • mysql 显示SQL语句执行时间的代码

    2024-01-16 03:25:14
  • 浅谈JavaScript函数节流

    2024-05-03 15:59:31
  • python识别围棋定位棋盘位置

    2023-01-09 01:49:34
  • 使用Go语言解决Scan空格结束输入问题

    2024-04-27 15:38:37
  • python将ip地址转换成整数的方法

    2022-10-17 23:40:23
  • 基于FlashPaper实现JSP在线阅读代码示例

    2023-06-14 21:02:31
  • Python实现的数据结构与算法之快速排序详解

    2022-03-03 16:49:17
  • python读写文件write和flush的实现方式

    2022-10-28 07:04:58
  • Python 一行代码能实现丧心病狂的功能

    2023-07-25 08:32:17
  • 解决MySQL8.0 输入无误仍然提示Access denied问题

    2024-01-16 06:00:37
  • Python实现两款计算器功能示例

    2023-01-18 06:18:39
  • Javascript removeChild()删除节点及删除子节点的方法

    2023-07-02 05:30:22
  • 使用Python脚本在Linux下实现部分Bash Shell的教程

    2023-10-02 06:55:28
  • Django Channel实时推送与聊天的示例代码

    2021-08-14 13:33:58
  • Python绑定方法与非绑定方法详解

    2021-04-12 00:20:19
  • Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件封装函数的方法

    2023-12-19 22:42:41
  • SQL Server从安装到建库为新手寻找捷径

    2009-01-13 13:22:00
  • 编写python代码实现简单抽奖器

    2023-04-07 12:32:48
  • asp之家 网络编程 m.aspxhome.com