激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 毛片视频在线免费观看 | 中文字幕在线亚洲精品 | 91看片国产| 欧美成人一级片 | bt 自拍 另类 综合 欧美 | 欧美成人一级 | 男女生羞羞视频网站在线观看 | 欧美一区2区三区4区公司二百 | 成年人黄视频 | 成人国产在线看 | 欧美日本在线播放 | 色婷婷久久一区二区 | 蜜桃精品视频在线观看 | asian gaysex| 吾色视频| 免费看操片| 韩国三级日本三级香港三级黄 | 国产美女精品视频 | 在线观看一二区 | 久久久久亚洲美女啪啪 | 一边吃奶一边插下面 | 污污的视频在线观看 | 成人免费毛片在线观看 | 美国黄色毛片女人性生活片 | 色阁阁69婷婷 | av不卡免费在线 | 国产午夜探花 | 19禁国产精品福利视频 | 国产欧美日韩一区二区三区四区 | 成人午夜网址 | 午夜免费网 | 国色天香综合网 | 九九热在线视频观看 | avhd101高清在线迷片麻豆 | 色多多视频导航 | 午夜国产在线观看 | 久久国产精品99国产 | 凹凸成人精品亚洲精品密奴 | 亚洲最新色 | 狠狠干五月天 | 久久99精品久久久久久园产越南 |