首页 > 解决方案 > 通过使用子字典中键的值对包含子字典的字典进行排序

问题描述

假设我有一个包含字典的字典,如下例所示:

a_dict = {
  'abcd': {'name': 'John', 'height': 180},
  'asdf': {'name': 'Lex', 'height': 160},
  'csxf': {'name': 'Amber', 'height': 193},
}

如何使用子字典中键的值对字典进行排序,即:'height'键的值?

标签: pythonsortingdictionary

解决方案


你可以试试这样的

dict(sorted(a_dict.items(), key=lambda item: item[1]["height"]))

推荐阅读