【问题标题】:Sorting list of dictionaries according to a certain key根据某个键对字典列表进行排序
【发布时间】:2015-02-17 06:32:03
【问题描述】:
    runnerList = [{1:{"name":"John","height":173,"age":16}},
                  {2:{"name":"Fred","height":185,"age":18}},
                  {3:{"name":"Luke","height":181,"age":17}}]

我一直在尝试根据跑步者的年龄按升序对这个字典列表进行排序,但没有成功:(我试过了:

    import operator
    runnerList.sort(key=lambda x: x['age'], reverse=True)

但似乎什么也没发生。我该怎么做?

【问题讨论】:

  • 因为你在另一个字典里面有一个字典。
  • 你应该得到一个KeyError,很难相信什么都没发生。
  • 您使用包含单元素升序整数键字典的字典是否有某些原因?这是一个相当奇特的数据结构。
  • 除非你有一些你没有提到的外部约束,否则像[{"name": "John", "height": 173, "age": 16}, {"name": "Fred", "height": 185, "age": 18}, {"name": "Luke", "height": 181, "age": 17}] 这样的简单字典列表会更容易处理。
  • 转换为@ZeroPiraeus [next(iter(x.values())) for x in runnerList]提出的数据模型

标签: python list sorting python-3.x dictionary


【解决方案1】:

试试这个:

runnerList.sort(key=lambda x: x.values()[0]['age'])
print runnerList

您收到错误的原因是您的相关字典(包含 age 键的字典)也包含在另一个字典中。解决方案很简单,获取外部 dictvalues() 并获取第一个元素 ([0]),它为您提供了其中包含的唯一字典。

另外,我删除了您示例中使用的reverse=True。它会按降序为您提供结果,并且您特别要求它们按升序

免责声明:

如果您使用的是 Python 版本 >= 3:,则必须转换使用 values() 创建的生成器对象。

runnerList.sort(key=lambda x: list(x.values())[0]['age'])

【讨论】:

  • @AvinashRaj 不在这里...你能说得更具体点吗?
【解决方案2】:

runnerList 是字典的字典列表。 如果您确定每个列表条目只有一对键值,那么您可以按以下方式排序:

runnerList.sort(key=lambda x: list(x.values())[0]['age'], reverse=True)

如果每个列表条目有多个键值对,那么您需要更好地指定排序逻辑。你会在这样的名单上做什么?:

runnerList = [{1:{"name":"John","height":173,"age":16}},
                  {  2:{"name":"Fred","height":185,"age":18},
                     4:{"name":"Fred","height":185,"age":13}
                  },
                  {3:{"name":"Luke","height":181,"age":17}}]

【讨论】:

  • TypeError: 'dict_values' object does not support indexing
  • @ZeroPiraeus 您的错误仅发生在 python 3 上。我在 2.7 上运行它。反正问题的答案还是一样的,问题是它是dict of dicts。
  • 问题标记为python-3.x
  • 你可以在 python 3 中做runnerList.sort(key=lambda x: list(x.values())[0]['age'], reverse=True)。我也会修复我的答案以在 3 上工作。 tnx
【解决方案3】:

您基本上拥有列表形式的 runnerList,因为它包含在 [] 中。首先将其更改为像

这样的字典
 runnerList = {1:{"name":"John","height":173,"age":16},
              2:{"name":"Fred","height":185,"age":18},
              3:{"name":"Luke","height":181,"age":17} }

然后你可以import operator 并可以使用下面的函数对项目进行排序并打印出来

sorted_runnerList = sorted(runnerList.items(),key=lambda x: x[1]['age'],)

虽然这会直接在字典中对字典中的年龄进行排序

所以基本上你的功能变成了

import operator
runnerList = {1:{"name":"John","height":173,"age":16},
              2:{"name":"Fred","height":185,"age":18},
              3:{"name":"Luke","height":181,"age":17} }

sorted_runnerList = sorted(runnerList.items(),key=lambda x: x[1]['age'],)
print sorted_runnerList

但请记住,当我们使用 .items() 时,它会返回不可变元组

输出

[(1, {'age': 16, 'name': 'John', 'height': 173}), (3, {'age': 17, 'name': 'Luke', 'height': 181}), (2, {'age': 18, 'name
': 'Fred', 'height': 185})]

【讨论】:

  • OP 想要按'age' 排序,而不是按键。
  • 它会自动按年龄排列。尝试在你自己的comp中运行程序
猜你喜欢
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-04
  • 2011-12-11
  • 1970-01-01
相关资源
最近更新 更多