简体   繁体   English

排序清单时发生Python错误

[英]Python error in sorting a list

I simply want to sort a list... and I have a 2 parameter lambda Here is my simple code: 我只是想对列表进行排序...,我有一个2参数lambda这是我的简单代码:

I.sort(key = lambda x, y: x.finish - y.finish)

And the compiler return this error 然后编译器返回此错误

builtins.TypeError: <lambda>() missing 1 required positional argument: 'y'

You are trying to use key function as a cmp function (removed in Python 3.x), but don't you mean to simply sort by the "finish" attribute: 您试图将key函数用作cmp函数(在Python 3.x中已删除),但不是要按“ finish”属性进行排序:

I.sort(key=lambda x: x.finish)

Or, with the "attrgetter" : 或者,使用“ attrgetter”

from operator import attrgetter

I.sort(key=attrgetter("finish"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM