Python使用key和lambda排序,lambda做什么? [英] Python sort using key and lambda, what does lambda do?

查看:212
本文介绍了Python使用key和lambda排序,lambda做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个像这样的值列表:

So I have a list of values like so:

{
    "values": 
    [
        {
            "date": "2015-04-15T11:15:34",
            "val": 30
        },
        {
            "val": 90,
            "date": "2015-04-19T11:15:34"
        },
        {
            "val": 25,
            "date": "2015-04-16T11:15:34"
        }
    ]
}

我正在使用pythons deafault json解析器解析成这样的列表:

that I'm parsing in with pythons deafault json parser into a list like so:

with open(file) as f:
    data = json.load(f)

values = data["values"]

然后我尝试按日期对数据进行排序,如下所示:

I'm then trying to sort the data by date like so:

values.sort(key=lambda values: values["date"])

这是可行的(据我所知).我的问题是为什么行得通?如果我无法访问values ["date"],那么为什么可以使用此lambda函数?值不能仅使用整数作为日期"之类的键.我的意思是,我只能访问这样的值:values [0],values [1]等...,因为它是一个列表,而不是字典.因此,如果此lambda函数等效于此:

And this works (to my knowledge). My question is why does it work? If I can't access values["date"] then why can I use this lambda function? values can't take a key like "date" only an integer. What I mean by this is I can only access values like so: values[0], values[1], etc... because it's a list not a dictionary. So if this lambda functions equivalent is this:

def some_method(values):
    return values["date"]

然后这是无效的,因为值是列表而不是字典.我无法访问值[日期"].

then this is invalid because values is a list not a dictionary. I can't access values["date"].

那么为什么我只能通过这样的函数传递日期呢?另外,如果您可以深入解释lambda,将不胜感激.我已经阅读了有关堆栈溢出的其他文章,但是对我来说它们根本没有意义.

So why can I just pass in the date through the function like this? Also if you could explain lambda in depth that would be appreciated. I've read other posts on stack overflow about it but they just don't make sense to me.

更新了问题,并提供了更多信息,以使问题更加清楚.

Updated question with more information to make the problem more clear.

推荐答案

lambda表达式只是编写函数的一种简洁方法.在您只需要使用一次函数的情况下,它特别方便,只需将其用作表达式(例如,函数的参数)即可.

A lambda expression is simply a concise way of writing a function. It's especially handy in cases like the one you give where you only need to use the function once, and it needs to be used as an expression (e.g. an argument to a function).

这是示例的替代版本,使用def语句而不是lambda表达式:

Here's an alternative version of your example, using def statement instead of a lambda expression:

def keyfunc(values):
    return values["date"]
values.sort(key=keyfunc)

那又长了两行,并且在当前名称空间中留下了一个额外的变量.如果lambda版本与def版本一样清晰,那么通常是更好的选择.

That's two lines longer, and leaves behind an extra variable in the current namespace. If the lambda version is just as clear as the def version, it's generally a better choice.

看起来您的困惑可能是由于函数中对名称values的额外使用.那只是一个错误选择的参数名称. list.sort方法将对列表中的每个值调用一次key函数,并将该值作为第一个位置参数传递.该值将绑定到函数声明中使用的任何变量名(无论它是def还是lambda).在您的示例中,更好的名称可能是valitem,因为它只是values列表中的一个项目.该名称实际上可以是您想要的任何名称(确实,values可以很好地工作,看起来有点令人困惑).这会更清楚:

It looks like your confusion may come from the extra use of the name values in the function. That's simply a poorly chosen argument name. The list.sort method will call the key function once for each value in the list, passing the value as the first positional argument. That value will be bound to whatever variable name is used in the function declaration (regardless of whether it's a def or a lambda). In your example, a better name might be val or item, since it's going to be just a single item from the values list. The name could really be whatever you want (and indeed, values works fine, it just looks confusing). This would be clearer:

values.sort(key=lambda val: val["date"])

或者:

def keyfunc(val):
    return val["date"]
values.sort(key=keyfunc)

这篇关于Python使用key和lambda排序,lambda做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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