Python reverse alphabetical order

后端 未结 3 751
庸人自扰
庸人自扰 2021-01-13 08:10

I have this output: [(3, \'one\'), (2, \'was\'), (2, \'two\'), (1, \'too\'), (1, \'racehorse\'), (1, \'a\')]

and i need to make it so that the tupl

3条回答
  •  盖世英雄少女心
    2021-01-13 09:08

    Define the list:

    >>> mylist = [(3, 'one'), (2, 'was'), (2, 'two'), (1, 'too'), (1, 'racehorse'), (1, 'a')]
    

    Sort the list:

    >>> sorted(mylist, key=lambda x: (-x[0], x[1]), reverse=True)
    [(1, 'too'), (1, 'racehorse'), (1, 'a'), (2, 'was'), (2, 'two'), (3, 'one')]
    

提交回复
热议问题