如果我匹配单独的键,迭代此数据集以从另一个键值对返回所有匹配值的最佳方法是什么?

问题描述 投票:0回答:3

我希望能够搜索这个字典列表(参见帖子底部)(我认为这就是这种特殊排列的名称),以搜索与“0xd2”匹配的[“地址”]。如果找到匹配项,我想返回/打印所有相应的 ['id']。

所以在这种情况下我想返回: 632、315、432、100

我能够像这样提取单个值:

none = None
print(my_dict['result'][2]["id"])

432

我正在努力解决如何让循环正确执行此操作。

{
  "total": 4,
  "page": 0,
  "page_size": 100,
  "result": [
    {
      "address": "0xd2",
      "id": "632",
      "amount": "1",
      "name": "Avengers",
      "group": "Marvel",
      "uri": "https://google.com/",
      "metadata": null,
      "synced_at": "2022-05-26T22:52:34.113Z",
      "last_sync": "2022-05-26T22:52:34.113Z"
    },
    {
      "address": "0xd2",
      "id": "315",
      "amount": "1",
      "name": "Avengers",
      "group": "Marvel",
      "uri": "https://google.com/",
      "metadata": null,
      "synced_at": "2022-05-26T22:52:34.113Z",
      "last_sync": "2022-05-26T22:52:34.113Z"
    },
    {
      "address": "0xd2",
      "id": "432",
      "amount": "1",
      "name": "Avengers",
      "group": "Marvel",
      "uri": "https://google.com/",
      "metadata": null,
      "synced_at": "2022-05-26T22:52:34.113Z",
      "last_sync": "2022-05-26T22:52:34.113Z"
    },
{
      "address": "0x44",
      "id": "100",
      "amount": "1",
      "name": "Suicide Squad",
      "group": "DC",
      "uri": "https://google.com/",
      "metadata": null,
      "synced_at": "2022-05-26T22:52:34.113Z",
      "last_sync": "2022-05-26T22:52:34.113Z"
    }
  ],
  "status": "SYNCED"
}
python dictionary-comprehension
3个回答
0
投票

您可以使用列表理解。

import json

json_string = """{
  "total": 4,
  "page": 0,
  "page_size": 100,
  "result": [
    {
      "address": "0xd2",
      "id": "632",
      "amount": "1",
      "name": "Avengers",
      "group": "Marvel",
      "uri": "https://google.com/",
      "metadata": null,
      "synced_at": "2022-05-26T22:52:34.113Z",
      "last_sync": "2022-05-26T22:52:34.113Z"
    },
    {
      "address": "0xd2",
      "id": "315",
      "amount": "1",
      "name": "Avengers",
      "group": "Marvel",
      "uri": "https://google.com/",
      "metadata": null,
      "synced_at": "2022-05-26T22:52:34.113Z",
      "last_sync": "2022-05-26T22:52:34.113Z"
    },
    {
      "address": "0xd2",
      "id": "432",
      "amount": "1",
      "name": "Avengers",
      "group": "Marvel",
      "uri": "https://google.com/",
      "metadata": null,
      "synced_at": "2022-05-26T22:52:34.113Z",
      "last_sync": "2022-05-26T22:52:34.113Z"
    },
{
      "address": "0x44",
      "id": "100",
      "amount": "1",
      "name": "Suicide Squad",
      "group": "DC",
      "uri": "https://google.com/",
      "metadata": null,
      "synced_at": "2022-05-26T22:52:34.113Z",
      "last_sync": "2022-05-26T22:52:34.113Z"
    }
  ],
  "status": "SYNCED"
}"""

json_dict = json.loads(json_string)

result = [elem['id'] for elem in json_dict['result'] if elem['address'] == '0xd2']

print(result)

输出:

['632', '315', '432']

0
投票

这会将关联的 ID 存储在列表中:

ids=[]
for r in dataset.get('result'):
    if r.get('address')=='0xd2':
        ids.append(r.get('id'))

0
投票

您可以尝试列表理解:

[res["id"] for res in my_dict["result"] if res["address"] == "0xd2"]

如果您想使用 for 循环:

l = []
for res in my_dict["result"]:
    if res["address"] == "0xd2":
        l.append(res["id"])
© www.soinside.com 2019 - 2024. All rights reserved.