统计分词法;词频统计;头歌

text = input()

text = text.lower()

# 将特殊字符替换成为空格

for ch in '!@#$%:^&*()-.;':

    text = text.replace(ch, " ")

# 对字符串通过空格进行分割

words = text.split()

counts = {}

# 任务:完成对text文本的词频统计,将结果保存到counts字典中

# ********** Begin *********#

for word in words:

    if word in counts:

        counts[word] = counts[word]+1  

    else:  

        counts[word]=1

 # ********** End **********#

items = list(counts.items())

items.sort(key=lambda x: x[1], reverse=True)

# 输出词频统计的结果

for i in range(3):

    word, count = items[i]

    if  i<2:

       print("{0}:{1}".format(word, count))

    else:

       print("{0}:{1}".format(word, count),end="")


版权声明:本文为qq_57063581原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。