# -*- 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)
# -*- 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)
的 : 17533次 是 : 8581次 不 : 6375次 我 : 6168次 了 : 5586次 一 : 5197次 这 : 4394次 在 : 4264次 有 : 4188次 人 : 4025次 一共耗时 : 0.5秒
的 : 17533次 是 : 8581次 不 : 6375次 我 : 6168次 了 : 5586次 一 : 5197次 这 : 4394次 在 : 4264次 有 : 4188次 人 : 4025次 一共耗时 : 0.280999898911秒
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有