【发布时间】:2022-01-15 17:58:36
【问题描述】:
我有一个这样的列表:
list_results=[('Horror', 2), ('Romance', 2), ('Comedy', 2), ('History', 2), ('Adventure', 1), ('Action', 3)]
我希望对数字进行降序排序,如果数字相同,则按照名称升序排序。
我尝试了以下代码:
sortlist=sorted(list_results,key=lambda x:(x[1],x[0]))
反之,但我想不通。
我正在寻找的答案是:
[('Action', 3), ('Comedy', 2) ,('History', 2),('Horror', 2), ('Romance', 2), ('Adventure', 1), ]
【问题讨论】:
标签: python sorting stable-sort