且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-07 23:26:48

使用

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 函数,它带有 2 个参数.

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