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...
什么是,什么是x,为什么[1]在x[1]中…
Examples
例子
max(gs_clf.grid_scores_, key=lambda x: x[1])
sort(mylist, key=lambda x: x[1])
3 个解决方案
#1
13
lambda
effectively 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]作为键。
#2
10
lambda
signifies an anonymous function. In this case, this function takes the single argument x
and returns x[1]
(i.e. the item at index 1 in x
).
lambda表示一个匿名函数。在本例中,该函数接受单个参数x并返回x[1](即在x中的索引1中的项)。
Now, sort(mylist, key=lambda x: x[1])
sorts mylist
based on the value of key
as 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 key
for each element.
现在,排序(mylist, key= x: x[1])根据应用于列表中的每个元素的键值对mylist进行排序。同样,max(gs_clf。key= x: x[1])返回gs_clf的最大值。grid_scores_关于每个元素的键返回的内容。
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)相当于您的键。
#3
2
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] i的列表。列表的第二元素到排序函数。基本上,我们把每个列表的第二个元素(i)相加。e x[1])到排序函数。我希望你能理解。
#1
13
lambda
effectively 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]作为键。
#2
10
lambda
signifies an anonymous function. In this case, this function takes the single argument x
and returns x[1]
(i.e. the item at index 1 in x
).
lambda表示一个匿名函数。在本例中,该函数接受单个参数x并返回x[1](即在x中的索引1中的项)。
Now, sort(mylist, key=lambda x: x[1])
sorts mylist
based on the value of key
as 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 key
for each element.
现在,排序(mylist, key= x: x[1])根据应用于列表中的每个元素的键值对mylist进行排序。同样,max(gs_clf。key= x: x[1])返回gs_clf的最大值。grid_scores_关于每个元素的键返回的内容。
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)相当于您的键。
#3
2
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] i的列表。列表的第二元素到排序函数。基本上,我们把每个列表的第二个元素(i)相加。e x[1])到排序函数。我希望你能理解。