题解 | #字符统计#
字符统计
https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
str1 = input() str2 = set(str1) d1 = {} for i in str2: d1[i] = str1.count(i) res = dict(sorted(d1.items(), key=lambda x: (-x[1], x[0]))) print("".join(res.keys()))
自己写了一版繁琐的,这一版是看别人的答案自己复现了一遍。lambda真香啊~
x: (-x[1], x[0]) 就是先按 x[1] 的倒序排序(负号就是reverse的意思);再按x[0]排序,也就是按ascii码默认顺序排序,以元组形式返回结果。6啊