简体   繁体   中英

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:

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:

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

Or, with the "attrgetter" :

from operator import attrgetter

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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