1. 程式人生 > >Python獲取一段文章中字母出現頻率前5的字母以及個數(去除空格、換行符等,只算字母)

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秒,比較慢