首页 > 解决方案 > 选择dict数组中的键

问题描述

您将如何从 python 3.x 中的 dict 数组中选择键和值?

输入:

[
  {
    "type": "update", "name":"joe"
  },
  {
    "type": "new", "name":"matthew"
  }
]

期望的输出:

[
  {
    "name":"joe"
  },
  {
    "name":"matthew"
  }
]

标签: python-3.xdictionary

解决方案


假设您想要'name'键及其值:

[{'name': item['name']} for item in original if 'name' in item]

推荐阅读