题解 | 字符统计

字符统计

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

from collections import Counter
s = Counter(input())
resls = list(s.items())
sorted_v = sorted(resls, key= lambda x : (-x[1], x[0]))
for _ in sorted_v:
    print(_[0],end='')



先用Counter记录 key 和 个数

然后排序

排序 sorted 的 key lambda 是 x:表示入参 value 表示要比较的结果,如果多个用元组隔开

全部评论

相关推荐

真烦好烦真烦:牛友太有实力了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务