问题:如何在Python中使用Lambda进行排序

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

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

我的电话是

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

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

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

The line I have is

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

回答 0

使用

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

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

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

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

Use

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

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

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

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


回答 1

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

它将打印如下:

[('apple', '10', '200'), ('baby', '20', '300'), ('candy', '30', '100')]
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')]

回答 2

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

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

根据您的要求,您可以选择以下两种:

如果要保留原始列表,可以使用排序功能;如果不需要原始列表,则可以使用排序功能。

在进行排序之前,我们需要了解lambda。

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

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

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

lambda参数:表达式

让我们看看如何使用排序功能:

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

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

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

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

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.

Before going on sort or sorted ,we need to understand 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.

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 arguments: expression

let’s see how to use sorted function:

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)]

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


回答 3

您正在尝试将关键函数与lambda函数一起使用。

Python和其他语言(例如C#或F#)使用lambda函数

另外,关于关键功能以及根据文档

无论list.sort()排序()具有一个关键参数来指定一个函数被调用之前,进行比较每个列表元件上。

key参数的值应该是一个采用单个参数并返回用于排序目的键的函数。该技术之所以快捷,是因为对于每个输入记录,键函数仅被调用一次。

因此,键函数具有参数键,并且确实可以接收lambda函数。

Real Python中,有一个很好的用法示例。假设您有以下清单

ids = ['id1', 'id100', 'id2', 'id22', 'id3', 'id30']

并希望对其“整数”进行排序。然后,你会做类似的事情

sorted_ids = sorted(ids, key=lambda x: int(x[2:])) # Integer sort

并打印它会给

['id1', 'id2', 'id3', 'id22', 'id30', 'id100']

在您的特定情况下,您只缺少key=在lambda之前写东西。因此,您需要使用以下内容

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

You’re trying to use key functions with lambda functions.

Python and other languages like C# or F# use lambda functions.

Also, when it comes to key functions and according to the documentation

Both list.sort() and sorted() have a key parameter to specify a function to be called on each list element prior to making comparisons.

The value of the key parameter should be a function that takes a single argument and returns a key to use for sorting purposes. This technique is fast because the key function is called exactly once for each input record.

So, key functions have a parameter key and it can indeed receive a lambda function.

In Real Python there’s a nice example of its usage. Let’s say you have the following list

ids = ['id1', 'id100', 'id2', 'id22', 'id3', 'id30']

and want to sort through its “integers”. Then, you’d do something like

sorted_ids = sorted(ids, key=lambda x: int(x[2:])) # Integer sort

and printing it would give

['id1', 'id2', 'id3', 'id22', 'id30', 'id100']

In your particular case, you’re only missing to write key= before lambda. So, you’d want to use the following

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。