【问题标题】:Filter Nested Dict With Key and Get Item in the dict使用键过滤嵌套字典并获取字典中的项目
【发布时间】:2020-09-02 18:10:26
【问题描述】:

我需要使用键 'name' 搜索列表字典并返回第一个匹配项的字典。例如,使用 'name' 搜索 'Sales' 并返回 dict 项。

second_step = [{'node': {'id': 'QWNjb3VudDo5NDI1MjA4NDI1NDAxMDgwMTQ7QnVzaW5lc3M6MGRmZDM5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 'name': 'Commodity Credit Loans', 'description': 'Income received from a Commodity Credit Corporation as part of a farm price and income support commodity program.', 'displayId': None, 'type': {'name': 'Income', 'value': 'INCOME'}, 'subtype': {'name': 'Income', 'value': 'INCOME'}, 'normalBalanceType': 'CREDIT', 'isArchived': False}}, {'node': {'id': 'QWNjb3VudDo5NDI1MjA4NDI2MjM5OTQwOTg7QnVzaW5lc3M6MGRmZDM5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 'name': 'Sales', 'description': 'Payments from your customers for products and services that your business sold.', 'displayId': None, 'type': {'name': 'Income', 'value': 'INCOME'}, 'subtype': {'name': 'Income', 'value': 'INCOME'}, 'normalBalanceType': 'CREDIT', 'isArchived': False}}, {'node': {'id': 'QWNjb3VudDo5NDI1MjA4NDI1ODIwNTEwNTY7QnVzaW5lc3M6MGRmZDM5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 'name': 'Services', 'description': 'Income received from professional services rendered to your customers.', 'displayId': None, 'type': {'name': 'Income', 'value': 'INCOME'}, 'subtype': {'name': 'Income', 'value': 'INCOME'}, 'normalBalanceType': 'CREDIT', 'isArchived': False}}]

我这样做了:

next((item for item in second_step if item["name"] == "Sales"), None)

我收到了这个错误:

Traceback (most recent call last):
  File "<pyshell#429>", line 1, in <module>
    next((item for item in second_step if item["name"] == "Sales"), None)
  File "<pyshell#429>", line 1, in <genexpr>
    next((item for item in second_step if item["name"] == "Sales"), None)
KeyError: 'name'

我希望以下内容作为结果返回。

{'id': 'QWNjb3VudDo5NDI1MjA4NDI2MjM5OTQwOTg7QnVzaW5lc3M6MGRmZDM5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 'name': 'Sales', 'description': 'Payments from your customers for products and services that your business sold.', 'displayId': None, 'type': {'name': 'Income', 'value': 'INCOME'}, 'subtype': {'name': 'Income', 'value': 'INCOME'}, 'normalBalanceType': 'CREDIT', 'isArchived': False}

我错过了什么?

【问题讨论】:

    标签: python dictionary


    【解决方案1】:

    您必须首先访问关键“节点”

    你可以这样做:

    next((item for item in second_step if item["node"]["name"] == "Sales"), None)
    

    【讨论】:

      【解决方案2】:
      next((item for item in second_step if item["node"]["name"] == "Sales"), None)
      

      【讨论】:

        【解决方案3】:

        问题是您正在检查item["name"],但在此之前还有一个名为"node" 的密钥。因此,您遇到了错误。

        好像你只返回 item 内部循环如下,

        next((item for item in second_step if item["node"]["name"] == "Sales"), None)
        

        你会得到

        {'node': {'id': 'QWNjb3VudDo5NDI1MjA4NDI2MjM5OTQwOTg7QnVzaW5lc3M6MGRmZDM5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 'name': 'Sales', 'description': 'Payments from your customers for products and services that your business sold.', 'displayId': None, 'type': {'name': 'Income', 'value': 'INCOME'}, 'subtype': {'name': 'Income', 'value': 'INCOME'}, 'normalBalanceType': 'CREDIT', 'isArchived': False}}
        

        因此,要获得所需的答案,您必须执行以下操作:

        next((item["node"] for item in second_step if item["node"]["name"] == "Sales"), None)
        

        这会给你输出为

        {'id': 'QWNjb3VudDo5NDI1MjA4NDI2MjM5OTQwOTg7QnVzaW5lc3M6MGRmZDM5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 'name': 'Sales', 'description': 'Payments from your customers for products and services that your business sold.', 'displayId': None, 'type': {'name': 'Income', 'value': 'INCOME'}, 'subtype': {'name': 'Income', 'value': 'INCOME'}, 'normalBalanceType': 'CREDIT', 'isArchived': False}
        

        【讨论】:

          猜你喜欢
          • 2021-05-10
          • 1970-01-01
          • 2011-05-13
          • 2016-01-07
          • 2017-06-07
          • 1970-01-01
          • 2013-02-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多