词频:单词出现的次数
f = open(r'D:pythonz','r',encoding='utf8')
data = f.read().lower()
data_split = data.split(' ')
count_dict = {}
for word in data_split:
if word not in count_dict:
count_dict[word] = 1
else:
count_dict[word] = count_dict[word] + 1
def func(i):
return i[1]
lt = list(count_dict.items())
lt.sort(key=func)
lt.reverse()
for i in lt[0:10]:
print(f'{i[0]:7}{i[1]5}')
结果:
to 21
you 10
the 15
a 13
your 2
if 3
that 3
be 3
negative3
yourself0