Python找出文件中使用率最高的汉字实例详解
作者:xm1331305 发布时间:2021-11-06 06:37:53
标签:Python,文件,汉字
本文实例讲述了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程序设计有所帮助。
0
投稿
猜你喜欢
- skimage包的exposure模块图像亮度与对比度的调整,是放在skimage包的exposure模块里面1、gamma调整对原图像的像
- 国外有很多优秀的文章可以用来学习,我决定花些时间翻译。我并不知道这篇文章有没有人翻译过,原文名 10 Awful IE Bugs and F
- import timenow_time = time.time()print(now_time)结果是1594
- 1、背景a、搜狗也发布了自己的人工智能 api,包括身份证ocr、名片ocr、文本翻译等API,初试感觉准确率一般般。b、基于python3
- 本文实例讲述了Python实现返回数组中第i小元素的方法。分享给大家供大家参考,具体如下:#! /usr/bin/env python#co
- 字体设计是人类商业活动的需求,它随着时代和科学技术的进步而不断地变化着。被广泛应用于网络生活的各个方面。现代字体设计在计算机技术的应用中已经
- 流式布局流式布局,也叫做瀑布流布局,是网页中经常使用的一种页面布局方式,它的原理就是将高度固定,然后图片的宽度自适应,这样加载出来的图片看起
- 如下所示:import osimport cv2import sysimport numpy as nppath = "F:\\I
- 问题如何遍历一个二叉树遍历二叉树就是访问二叉树的每一个节点二叉树父结点下先左访问,先序遍历(根左右)例如:遍历以下的二叉树遍历结果:ABDE
- 如何搭建完整的网站架构并设计出一个出色的网站?关于这个问题,我们很难提出一个绝对权威和正确的设计思路,但任何网站的设计都需要遵循一个循序渐进
- 随机背景--当你每次进入该页面时,从已指定的图片文件夹中,随机选取一个图片作为背景显示。这里介绍的方法是用ASP+CSS来实现的。 &nbs
- 一:需重定义神经网络继续训练的方法1.训练代码import numpy as npimport tensorflow as tfx_data
- 生活中有很多需要用到关联图的地方,至少我认为的是这样的图:https://www.echartsjs.com/examples/zh/edi
- strConnString = "driver={MySQL ODBC 3.51 
- 转自微信公众号:机器学习社区,经作者授权转载时间序列分析是数据科学中一个非常重要的领域,它主要包含统计分析、检测变化点、异常检测和预测未来趋
- import wx import imagesclass DemoTaskBarIcon(wx.TaskBarIcon): &nb
- 喜欢Gucci的优雅吗?或者痴迷美国普普艺术?谷歌中国最近改版的谷歌个性化首页iGoogle集中了近1500个主题,包括近120多位全球顶级
- 在命令行中使用 Python 时,它可以接收大约 20 个选项(option),语法格式如下:python [-bBdEhiIOqsSuvV
- 本文实例讲述了Python二叉树的镜像转换实现方法。分享给大家供大家参考,具体如下:问题描述操作给定的二叉树,将其变换为源二叉树的镜像。思路
- 看代码吧~predict = output.argmax(dim = 1)confusion_matrix =torch.zeros(2,2