简体   繁体   English

使用 Sorted function 和 lambda 进行排序

[英]Sorting with Sorted function and lambda

Found this code snippet which is sorting a list of items by display_order:找到了这个代码片段,它按 display_order 对项目列表进行排序:

conditions = sorted(conditions,
                    key=lambda x: (x.display_order, x.condition_guid != condition.condition_guid))

Where conditions is a list:条件是一个列表:

conditions: [
        Conditions 49, 015c0dec-26b8-4343-93fc-27d36548ae0a, 
        Conditions 48, 0bab9d83-b730-4c23-986b-e3ff5b0240a2, 
        Conditions 50, 7cd47bd1-0073-45a4-9793-3ed83f93b213, 
        Conditions 51, ce684f3c-954e-43af-8289-77c4cd835131, 
        Conditions 52, 8fce7db6-6813-4a46-a56c-731b7eb2d4bf]

Sorted function follows this syntax:排序后的 function 遵循以下语法:

sorted(iterable, key=None, reverse=False)

where iterable = conditions.其中可迭代=条件。

Wondering the meaning of the parenthesis and comma in the lambda function:想知道lambda function中括号和逗号的含义:

(x.display_order, x.condition_guid != condition.condition_guid)

key in sorted does not have to be a single value, it can be a set to provide secondary sorting characteristics. sorted中的key不一定是单个值,它可以是一个集合以提供二次排序特性。

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

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