Given a list of dictionaries, how can I eliminate duplicates of one key, and sort by another

前端 未结 7 904
清歌不尽
清歌不尽 2021-01-02 02:31

I\'m working with a list of dict objects that looks like this (the order of the objects differs):

[
    {\'name\': \'Foo\', \'score         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 03:10

    Just for fun, here is a purely functional approach:

    >>> map(dict, dict(sorted(map(sorted, map(dict.items, s)))).items())
    [{'score': 3, 'name': 'Bar'}, {'score': 2, 'name': 'Baz'}, {'score': 3, 'name': 'Foo'}]
    

提交回复
热议问题