这是什么意思:key=lambda x: x[1] ? [英] What does this mean: key=lambda x: x[1] ?

查看:212
本文介绍了这是什么意思:key=lambda x: x[1] ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到它用于排序,但是这行代码的各个组件实际上是什么意思?

I see it used in sorting, but what do the individual components of this line of code actually mean?

key=lambda x: x[1]

什么是 lambda,什么是 x:,为什么 [1]x[1] 等...

What's lambda, what is x:, why [1] in x[1] etc...

示例

max(gs_clf.grid_scores_, key=lambda x: x[1])

sort(mylist, key=lambda x: x[1])

推荐答案

lambda 有效地创建了一个内联函数.例如,你可以重写这个例子:

lambda effectively creates an inline function. For example, you can rewrite this example:

max(gs_clf.grid_scores_, key=lambda x: x[1])

使用命名函数:

def element_1(x):
    return x[1]

max(gs_clf.grid_scores_, key=element_1)

在这种情况下,max() 将返回该数组中第二个元素 (x[1]) 大于所有其他元素的第二个元素的元素元素.另一种表述方式是函数调用所暗示的:返回最大元素,使用 x[1] 作为 key.

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.

这篇关于这是什么意思:key=lambda x: x[1] ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆