Python 这是什么意思:key=lambda x: x[1] ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16310015/
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
What does this mean: key=lambda x: x[1] ?
提问by Nyxynyx
I see it used in sorting, but what do the individual components of this line of code actually mean?
我看到它用于排序,但是这行代码的各个组件实际上是什么意思?
key=lambda x: x[1]
What's lambda, what is x:, why [1]in x[1]etc...
什么是lambda什么x:,为什么[1]在x[1]等等...
Examples
例子
max(gs_clf.grid_scores_, key=lambda x: x[1])
sort(mylist, key=lambda x: x[1])
回答by cdhowie
lambdaeffectively creates an inline function. For example, you can rewrite this example:
lambda有效地创建了一个内联函数。例如,您可以重写此示例:
max(gs_clf.grid_scores_, key=lambda x: x[1])
Using a named function:
使用命名函数:
def element_1(x):
return x[1]
max(gs_clf.grid_scores_, key=element_1)
In this case, max()will return the element in that array whose second element (x[1]) is larger than all of the other elements' second elements. Another way of phrasing it is as the function call implies: return the max element, using x[1]as the key.
在这种情况下,max()将返回该数组中第二个元素 ( x[1]) 大于所有其他元素的第二个元素的元素。另一种表述方式是函数调用所暗示的:返回 max 元素,x[1]用作key。
回答by arshajii
lambdasignifies an anonymous function. In this case, this function takes the single argument xand returns x[1](i.e. the item at index 1 in x).
lambda表示匿名函数。在这种情况下,此函数采用单个参数x并返回x[1](即索引 1 in 处的项目x)。
Now, sort(mylist, key=lambda x: x[1])sorts mylistbased on the value of keyas applied to each element of the list. Similarly, max(gs_clf.grid_scores_, key=lambda x: x[1])returns the maximum value of gs_clf.grid_scores_with respect to whatever is returned by keyfor each element.
现在,根据应用于列表中每个元素的 as值进行sort(mylist, key=lambda x: x[1])排序。类似地,返回相对于每个元素返回的任何内容的最大值。mylistkeymax(gs_clf.grid_scores_, key=lambda x: x[1])gs_clf.grid_scores_key
I should also point out that this particular function is already included in one of the libraries: operator. Specifically, operator.itemgetter(1)is equivalent to your key.
我还应该指出,这个特定的函数已经包含在一个库中:operator. 具体来说,operator.itemgetter(1)相当于你的key.
回答by Preeti
distances.sort(key=lambda x: x[1])
This is the function.
And here x is the list, in which we are adding x[1]i.e 2nd element of list to the sort function. So, basically we are adding every list's 2nd element (i.e x[1]) to the sort function.
I hope you understand this.
这就是功能。这里 x 是列表,我们将x[1]列表的第二个元素添加到排序函数中。所以,基本上我们将每个列表的第二个元素(即 x[1])添加到排序函数中。我希望你明白这一点。
回答by WANNISA RITMAHAN
From a reference for Python 3.7 (https://docs.python.org/3/howto/sorting.html), The key is a parameter of list.sort()and sorted(). The first built-in function modifies a list in place while the latter accepts and return iterable.
来自 Python 3.7 的参考 ( https://docs.python.org/3/howto/sorting.html),键是list.sort()and的参数sorted()。第一个内置函数就地修改列表,而后者接受并返回可迭代对象。
The key parameter can be defined as a function to be called on each element of list/iterable before comparison and sort, respectively. In this case, the inline function lambda x: x[1]is defined as a value of the key parameter. The lambda function takes input x return x[1] which is the second element of x.
key 参数可以定义为一个函数,分别在比较和排序之前在 list/iterable 的每个元素上调用。在这种情况下,内联函数lambda x: x[1]被定义为 key 参数的值。lambda 函数接受输入 x 返回 x[1],这是 x 的第二个元素。
Supposed
应该
mylist = [[7, 8], [1, 2, 3], [2, 5, 6]]
# list(map(lambda x: x[1], mylist)) returns [8, 2 ,5]
mylistSort = sorted(mylist, key = lambda x: x[1])
# will sort the nested list based on the result of the lambda function
Can you guess what the result? mylistSort is then [[1,2,3], [2,5,6], [7,8]] from the sorted sequence of [8,2,5] which is [2,5,8].
你能猜出结果是什么吗?mylistSort 然后是 [[1,2,3], [2,5,6], [7,8]] 来自 [8,2,5] 的排序序列,即 [2,5,8]。
The max()in your example is applied to just get the max value from the outcome of the sort function.
将max()在您的示例应用到刚刚得到的排序功能的结果的最大值。
I hope this post is helpful.
我希望这篇文章有帮助。
回答by filler36
One more example of usage sorted()function with key=lambda. Let's consider you have a list of tuples. In each tuple you have a brand, model and weight of the car and you want to sort this list of tuples by brand, model or weight. You can do it with lambda.
使用sorted()函数和 key=lambda 的另一个例子。假设您有一个元组列表。在每个元组中,您都有汽车的品牌、型号和重量,并且您希望按品牌、型号或重量对这个元组列表进行排序。你可以用 lambda 来做到这一点。
cars = [('citroen', 'xsara', 1100), ('lincoln', 'navigator', 2000), ('bmw', 'x5', 1700)]
print(sorted(cars, key=lambda car: car[0]))
print(sorted(cars, key=lambda car: car[1]))
print(sorted(cars, key=lambda car: car[2]))
Results:
结果:
[('bmw', 'x5', 1700), ('citroen', 'xsara', 1100), ('lincoln', 'navigator', 2000)]
[('lincoln', 'navigator', 2000), ('bmw', 'x5', 1700), ('citroen', 'xsara', 1100)]
[('citroen', 'xsara', 1100), ('bmw', 'x5', 1700), ('lincoln', 'navigator', 2000)]
回答by omkar
student_tuples = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),]
sorted(student_tuples, key=lambda student: student[2]) # sort by age
>>>>[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
from official documentation
来自官方文档

