import collections
import time
def gettf(x):
res=dict()
for t in x:
res[t]=res.get(t,0)+1
return res
with open('book.txt',"r",encoding='UTF-8') as f:
text=f.read()
splitedText_list=text.split(' ')#list
time_start=time.time()
s=collections.Counter(splitedText_list)
time_end=time.time()
print('time cost',time_end-time_start,'s')
time_start=time.time()
s2=gettf(splitedText_list)
time_end=time.time()
print('time cost2',time_end-time_start,'s')
print(s["the"])
print(s2["the"])
我这里测试大概差两倍时间