版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41672744/article/details/79682950
统计词频:
import time
name = input("Enter file:")
start =time.clock()
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
di=dict()
print (di)
for line in handle:
lines=line.rstrip()
#print(lines)
wds= lines.split()
#print(wds)
for w in wds:
# print(w)
di[w]=di.get(w,0)+1
# print(di[w])
#print(w)
print (di)
end = time.clock()
print('Running time: %s Seconds'%(end-start))
python给字典的元素的值初始化的方法:
1. 常规方法 if else
for w in wds:
if w in di:
di[w] = di[w]+1
else:
di[w] = 1
2. 字典的"get”方法
for w in wds:
di[w]=di.get(w,0)+1
常用于利用字典统计单词频率