当前位置:   article > 正文

统计分词法;词频统计;头歌_text = input() text = text.lower() # 将特殊字符替换成为空格 f

text = input() text = text.lower() # 将特殊字符替换成为空格 for ch in '!@#$%

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="")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/701085
推荐阅读
相关标签
  

闽ICP备14008679号