如何在 Python 中使用 lambda 进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3766633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:39:16  来源:igfitidea点击:

How to sort with lambda in Python

pythonlambda

提问by Niklas

In Python, I am trying to sort by date with lambda. I can't understand my error message. The message is:

在 Python 中,我试图用 lambda 按日期排序。我无法理解我的错误信息。消息是:

<lambda>() takes exactly 1 argument (2 given)

The line I have is

我的线路是

a = sorted(a, lambda x: x.modified, reverse=True)

采纳答案by kennytm

Use

a = sorted(a, key=lambda x: x.modified, reverse=True)
#             ^^^^

On Python 2.x, the sortedfunction takes its arguments in this order:

在 Python 2.x 上,该sorted函数按以下顺序接受其参数:

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

so without the key=, the function you pass in will be considered a cmpfunction which takes 2 arguments.

因此,如果没有key=,您传入的函数将被视为带有cmp2 个参数的函数。

回答by Trillionaire Sanai

lst = [('candy','30','100'), ('apple','10','200'), ('baby','20','300')]
lst.sort(key=lambda x:x[1])
print(lst)

It will print as following:

它将打印如下:

[('apple', '10', '200'), ('baby', '20', '300'), ('candy', '30', '100')]

回答by Preeti Duhan

Python lists have two built-in ways to sort data:

Python 列表有两种内置的数据排序方式:

sort() — A method that modifies the list in-place
sorted() — A built-in function that builds a new sorted list from an iterable

Based on your requirement you can choose among these two:

根据您的要求,您可以在以下两个中进行选择:

if you want to keep original list ,you can use sorted function or if you don't need original list you can use sort function.

如果你想保留原始列表,你可以使用 sorted 函数,或者如果你不需要原始列表,你可以使用 sort 函数。

Before going on sort or sorted ,we need to understand lambda.

在进行 sort 或 sorted 之前,我们需要了解 lambda。

A lambda is an anonymous function and an anonymous function is a function that is defined without a name, this post seems to explain it pretty nicely.

一个 lambda 是一个匿名函数,一个匿名函数是一个没有名称定义的函数,这篇文章似乎很好地解释了它。

https://www.programiz.com/python-programming/anonymous-function

https://www.programiz.com/python-programming/anonymous-function

Lambda functions are nice for calling in-line because they only have one expression which is evaluated and returned. They syntax for a lambda is:

Lambda 函数非常适合内联调用,因为它们只有一个被评估和返回的表达式。它们的 lambda 语法是:

lambda arguments: expression

lambda 参数:表达式

let's see how to use sorted function:

让我们看看如何使用 sorted 函数:

student_tuples = [('john', 'A', 15),('jane', 'B', 12),('dave', 'B', 10),]
sorted(student_tuples, key=lambda student: student[2]) 

output: [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

输出: [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

Here we can see list student_tuples having tuples is sorted based on key parameter provided that is student[2].

在这里,我们可以看到具有元组的列表 student_tuples 是根据关键参数进行排序的,前提是 student[2]。