How to sort with lambda in Python

前端 未结 4 1518
感情败类
感情败类 2020-11-30 19:41

In Python, I am trying to sort by date with lambda. I can\'t understand my error message. The message is:

() takes exactly 1 argument (2 given         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 20:36

    Use

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

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

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

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

提交回复
热议问题