一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python找出文件中使用率最高的漢字實例詳解

Python找出文件中使用率最高的漢字實例詳解

2020-07-12 11:10xm1331305 Python

這篇文章主要介紹了Python找出文件中使用率最高的漢字,涉及Python針對字符串與中文的相關操作技巧,需要的朋友可以參考下

本文實例講述了Python找出文件中使用率最高的漢字的方法。分享給大家供大家參考。具體分析如下:

這是我初學Python時寫的,為了簡便,我并沒在排序完后再去掉非中文字符,稍微會影響性能(大約增加了25%的時間)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# -*- 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的可讀性更高的話,這樣也是可以的:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# -*- 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)

或者讓行數更少(好囧的列表綜合):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
# -*- 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者性能幾乎一樣,結果如下:

?
1
2
3
4
5
6
7
8
9
10
11
的 : 17533
是 : 8581
不 : 6375
我 : 6168
了 : 5586
一 : 5197
這 : 4394
在 : 4264
有 : 4188
人 : 4025
一共耗時 : 0.5

引入psyco模塊的成績:

?
1
2
3
4
5
6
7
8
9
10
11
的 : 17533
是 : 8581
不 : 6375
我 : 6168
了 : 5586
一 : 5197
這 : 4394
在 : 4264
有 : 4188
人 : 4025
一共耗時 : 0.280999898911

 

注:測試文件為778KB的GBK編碼,40余萬字。

希望本文所述對大家的Python程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美高清无砖专区欧美精品 | bedfriend泰剧全集免费观看 | 色久天| 欧美日韩一区二区三区在线播放 | 深夜在线| 北海市副市长黄江老公 | 国产99久久九九精品免费 | 国产永久在线观看 | 女人和拘做受全过程免费 | 蜜桃久久久亚洲精品成人 | 美国雪白人妖sarina | 男人天堂bt| 国产一区二区精品久 | 逼中极品| 欧美聚众性派对hdsex | 国产精品天天看天天爽 | 污网站免费观看在线高清 | 午夜精品久久久久久 | 99久久国产综合精品女不卡 | 欧美男同videos | 欧式午夜理伦三级在线观看 | 黑人又大又硬又粗再深一点 | 国产免费视 | 成人在线播放 | 精品视频久久久久 | 白丝尤物的下面被疯狂蹂躏 | 日本精品一卡二卡≡卡四卡 | 成年女人毛片免费观看中文w | 亚洲欧美久久婷婷爱综合一区天堂 | 精品夜夜澡人妻无码AV蜜桃 | 激情视频图片小说qvdo | 欧美成人aletta ocean | 大肚孕妇的高h辣文 | 国产成人综合网亚洲欧美在线 | 扒开双腿疯狂进出爽爽动态图 | 久久亚洲精品中文字幕60分钟 | 95在线观看精品视频 | 四虎成人影院网址 | 亚洲午夜精品久久久久久抢 | 欧美日本一区视频免费 | 精品无码一区二区三区中文字幕 |