Python题解 | #字符统计#

字符统计

https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0

import sys
from collections import Counter

while True:
    try:
        s = input()
        arr = Counter(s)
        dict1 = sorted(arr.items(), key=lambda x: x[0], reverse=False)
        dict1 = sorted(dict1, key=lambda x: x[1], reverse=True)
        
        for i in dict1:
            print(i[0], end='')


    except:
        break

全部评论
字典排序dict.items key=lambda x: x[0] reverse=False先按照key排序,生成一个list,再按照list的方式对value排序即可注意:items可以将字典转换为list,list不可以用items
点赞
送花
回复 分享
发布于 05-18 22:45 上海

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务