Sort nested dictionary by value, and remainder by another value, in Python

前端 未结 4 1537
梦谈多话
梦谈多话 2020-11-30 03:11

Consider this dictionary format.

{\'KEY1\':{\'name\':\'google\',\'date\':20100701,\'downloads\':0},
 \'KEY2\':{\'name\':\'chrome\',\'date\':20071010,\'downlo         


        
4条回答
  •  猫巷女王i
    2020-11-30 03:29

    Use the key argument for sorted(). It lets you specify a function that, given the actual item being sorted, returns a value that should be sorted by. If this value is a tuple, then it sorts like tuples sort - by the first value, and then by the second value.

    sorted(your_list, key=lambda x: (your_dict[x]['downloads'], your_dict[x]['date']))
    

提交回复
热议问题