Python获取一段文章中字母出现频率前5的字母以及个数(去除空格、换行符等,只算字母)

import time,re
from collections import Counter


text = 'A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.A friend of mine named Paul received an automobile from his brother as Christmas present. On Christmas eve, when Paul came out of his office, a street urchin was walking around the shining new car, admiring it.'
result = re.findall('[a-zA-Z]', text.lower())

t1 = time.time()
for i in range(0,1000):
	dict = {}
	s = set(result) # 集合:得到不重复的元素
	for i in s:
	    dict[i] = result.count(i) # 对集合中每个元素分别计数,存入dictionary中
	testlist = sorted(dict.items(),key = lambda x:x[1],reverse = True)
t2 = time.time()
print(testlist[0:5])
print ("first method:",t2-t1)


t1 = time.time()
for i in range(0,1000):
	count = Counter(result)
	testlist = sorted(count.items(),key = lambda x:x[1],reverse = True)
t2 = time.time()
print(testlist[0:5])
print("second method:",t2-t1)

执行结果:

[('e', 760), ('i', 640), ('a', 640), ('n', 560), ('r', 520)]
first method: 6.3350348472595215
[('e', 760), ('i', 640), ('a', 640), ('n', 560), ('r', 520)]
second method: 0.7251110076904297

显然第二种使用Counter的方法速度更快!

另外用max试了一下返回出现次数最多的

t1 = time.time()
for i in range(0,10):
	test=max(result, key=result.count)
t2 = time.time()
print(test)
print ("third method:",t2-t1)

结果:

e
third method: 21.224278211593628

运行10次需要21秒多,运行一次大概需要2秒,比较慢

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章