700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Python-统计一段文字中的单词个数并按单词的字母顺序排序后输出

Python-统计一段文字中的单词个数并按单词的字母顺序排序后输出

时间:2024-01-17 04:03:04

相关推荐

Python-统计一段文字中的单词个数并按单词的字母顺序排序后输出

现需要统计若干段文字(英文)中的不同单词数量。

如果不同的单词数量不超过10个,则将所有单词输出(按字母顺序),否则输出前10个单词。

注1:单词之间以空格(1个或多个空格)为间隔。

注2:忽略空行或者空格行。

注3:单词大小写敏感,即’word’与’WORD’是两个不同的单词 。

输入说明

若干行英文,最后以!!!为结束。

输出说明

不同单词数量。 然后输出前10个单词(按字母顺序),如果所有单词不超过10个,则将所有的单词输出。

输入样例

Failure is probably the fortification in your pole

It is like a peek your wallet as the thief when you

are thinking how to spend several hard-won lepta

when you Are wondering whether new money it has laid

background Because of you, then at the heart of the

most lax alert and most low awareness and left it

godsend failed

!!!

输出样例

49

Are

Because

Failure

It

a

alert

and

are

as

at

两种实现方法:

"""str=""while True:words=input()if words=="!!!!!":breakstr=str+" "+wordsstr_list=[]i=0for s in str.split():if s not in str_list:str_list.append(s)i+=1print(i)str_list.sort()if len(str_list)<10:for s in str_list:print(s)else:for s in range(10):print(str_list[s])"""str_list=[]n=0while True:str=input()if str=="!!!!!":breakfor i in str.split(" "):if len(i)>0 and i not in str_list:str_list.append(i)n+=1print(n)str_list.sort()if len(str_list)>10:for i in range(10):print(str_list[i])else:for i in str_list:print(i)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。