Python 字符串统计排序 - 按出现次数和 ASCII 码排序输出
def count_and_sort(s): d = {} for c in s: if c in d: d[c] += 1 else: d[c] = 1 items = list(d.items()) items.sort(key=lambda x: (-x[1], ord(x[0]))) # 按照统计个数和ASCI码排序 result = '' for item in items: result += item[0] * item[1] return result
输入字符串
s = input('请输入字符串:')
输出排序后的字符串
print(count_and_sort(s))
原文地址: https://www.cveoy.top/t/topic/nUsA 著作权归作者所有。请勿转载和采集!