综合练习:词频统计

song = '''
Twinkle, twinkle, little star, How I wonder what you are. Up above the world so high, Like a diamond in the sky. Twinkle, twinkle, little star, How I wonder what you are! When the blazing sun is gone, When he nothing shines upon, Then you show your little light, Twinkle, twinkle, all the night. Twinkle, twinkle, little star, How I wonder what you are
'''


#分隔符全部替换为空格,大写转换为小写,以空格划分每个单词
str1 = song.replace('',' ').lower().split()
str2 = song.split()

#统计各单词出现的次数
c = {}
for i in str2:
    count = str1.count(i)
    c[i] = count

#去掉没意义的单词
word = '''
i you you're the a are in what how
'''
str3 = word.split()
for i in str3:
    if i in c.keys():
        del (c[i])

#排序
count = sorted(c.items(),key=lambda items: items[1], reverse=True)

#输出词频最大10
for i in range(10):
    print(count[i])

原文地址:https://www.cnblogs.com/wollen-zwt/p/8649602.html