首页 > 试题广场 >

单词识别

[编程题]单词识别
  • 热度指数:15439 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入一个英文句子,把句子中的单词(不区分大小写)按出现次数按从多到少把单词和次数在屏幕上输出来,次数一样的按照单词小写的字典序排序输出,要求能识别英文单词和句号

输入描述:
输入为一行,由若干个单词和句号组成


输出描述:
输出格式参见样例。
示例1

输入

A blockhouse is a small castle that has four openings through which to shoot.

输出

a:2
blockhouse:1
castle:1
four:1
has:1
is:1
openings:1
shoot:1
small:1
that:1
through:1
to:1
which:1
from collections import Counter

word=input().split()
# 去句号
if word[-1][len(word[-1])-1] =='.':
    word[-1]=word[-1][0:len(word[-1])-1]

# 转小写
words = [i.lower() for i in word]
# 统计词频
dic = Counter(words)

dic=sorted(dic.items(),key=lambda x:(x[0],x[1]),reverse=True)
#for kv in dic:
#print('{}:{}'.format(kv[0],kv[1]))
# 反向遍历
for x in dic[::-1]:
    print("{}:{}".format(x[0],x[1]))


编辑于 2024-03-19 20:58:00 回复(0)

问题信息

上传者:小小
难度:
2条回答 7273浏览

热门推荐

通过挑战的用户

查看代码