7-4 jmu-Java&Python-统计一段文字中的单词个数并按单词的字母顺序排序后输出 (10 分)

题目要求

现需要统计若干段文字(英文)中的不同单词数量。
如果不同的单词数量不超过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

题解

本题重点在如何将重复冗余去掉,然后按照字母排序(可以采用列表的set()函数,默认升序),输出对应个数单词

重要思想

  • 删除列表中重复冗余方法
ls = []
a = ["one", "two", "three", "two"]
for item in a:
    if item not in ls:
        ls.append(item)

完整代码

count = 0
ls = []
while True:
    s = input()
    if s == "!!!!!":
        break
    a = s.split()
    for item in a:             #删除列表重复冗余
        if item not in ls:
            count = count + 1
            ls.append(item)
ls.sort()           #列表排序   
print(count)
if count <= 10:
    for i in range(count):
        print(ls[i])
else:
    for i in range(10):
        print(ls[i])
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值