繁体   English   中英

如何对三个 Vals 的压缩列表进行排序,其中我想要前两个反转(降序)而第三个不是(升序)?

[英]How do I Sort a Zipped List of Three Vals where I want first two Reversed (Descending) and Third is not (Ascending)?

我正在尝试对三个已压缩的列表进行排序。 我希望它对前两列(从最高到最低)进行反向排序,然后按升序排列第三列(从最低到最高)。 所以在下面的例子中,首先是最高的世界排名,然后是最高分,然后是名字,按字典顺序较短的名字在前。

我可以使用以下内容完成第一部分:

names = ["name1", "McNamealot", "Namey Name Name", "Namey", "afirst"]
scores = [1,3,42,42,5]
world_rankings = [850,750,650,550,450]

zipped = zip(world_rankings, scores, names)

def sorter(item):
    return item[0], item[1] 
            
zipperoni = sorted(zipped, key = sorter, reverse = True)

但是,我无法弄清楚如何在不覆盖前两列的正确排序的情况下获得第三列。

我确定我缺少一些基本概念。 任何答案和ofc解释,将不胜感激。

正确答案:

sorted(zipped, key=lambda item: (-item[0], -item[1], item[2]))

感谢机械猪的超快反应!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM