How do I sort this list of tuples by both values?

前端 未结 4 985
被撕碎了的回忆
被撕碎了的回忆 2021-01-20 15:58

I have a list of tuples: [(2, Operation.SUBSTITUTED), (1, Operation.DELETED), (2, Operation.INSERTED)]

I would like to sort this list in 2 ways:

4条回答
  •  情书的邮戳
    2021-01-20 16:27

    You can use this:

    from operator import itemgetter
    d = [(1, 'DELETED'), (2, 'INSERTED'), (2, 'SUBSTITUTED')]
    d.sort(key=itemgetter(1),reverse=True)
    d.sort(key=itemgetter(0))
    print(d)
    

提交回复
热议问题