Sorting a dictionary of tuples in Python

后端 未结 2 1441
野性不改
野性不改 2021-02-20 13:55

I know there\'s tonnes of questions on python sorting lists/dictionaries already, but I can\'t seem to find one which helps in my case, and i\'m looking for the most efficient s

2条回答
  •  既然无缘
    2021-02-20 14:41

    Something like this?

    >>> a = {'a': (1, 2, 3), 'b': (3, 2, 1)}
    >>> b = a.items()
    >>> b
    [('a', (1, 2, 3)), ('b', (3, 2, 1))]
    >>> b.sort(key=lambda x:x[1][2])  # sorting by the third item in the tuple
    >>> b
    [('b', (3, 2, 1)), ('a', (1, 2, 3))]
    

提交回复
热议问题