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

编程入门 行业动态 更新时间:2024-10-10 00:30:22
本文介绍了如何在Python中使用Lambda进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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)

我的电话是

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

推荐答案

使用

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

在Python 2.x上,sorted函数按以下顺序获取其参数:

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

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

因此,如果没有key=,则传入的函数将被视为具有两个参数的cmp函数.

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

更多推荐

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

本文发布于:2023-11-28 04:48:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641014.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Python   Lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!