综合练习:词频统计

代码如下:

 

# -*- coding:utf-8 -*-

fo=open('/Users/Administrator/Desktop/song.txt','r')
song=fo.read()
fo.close()

symbol = list(''',.?!’:"“”-%$''')
for i in symbol:
    song = song.replace(i, ' ')
song = song.lower()
split = song.split()
word = {}
for i in split:
    count = song.count(i)
    word[i] = count
words = '''
a an the in on to at and of is was are were i he she you your they us their our it or for be too do no 
that s so as but it's
'''
prep = words.split()
for i in prep:
    if i in word.keys():
        del(word[i])
word = sorted(word.items(), key=lambda item: item[1], reverse=True)
for i in range(10):
    print(word[i])

 

 

运行截图:

 

posted @ 2018-03-26 10:58  131李锦旭  阅读(84)  评论(0编辑  收藏  举报