from collections import Counter c = Counter(input()) result = sorted(c.items(), key=lambda x: (-x[1], x[0])) print("".join(x[0] for x in result))
例如result = [('c', 3), ('d', 3), ('a', 2)]
用-x[1]就可以改成按数字降序, x[0]按字母升序, sorted 条件按顺序来的