简体   繁体   English

使用键过滤嵌套字典并获取字典中的项目

[英]Filter Nested Dict With Key and Get Item in the dict

I need to search through the list dict with the key 'name' and return the dict of the first match.我需要使用键“名称”搜索列表字典并返回第一个匹配项的字典。 Eg Search for 'Sales' using 'name' and return the the dict items.例如,使用 '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}}]

I did this:我这样做了:

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

I got this error:我收到了这个错误:

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'

I want the below to return as result.我希望以下内容作为结果返回。

{'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}

What am I missing?我错过了什么?

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

The problem was you were checking item["name"] but before that there is a another key named "node" .问题是您正在检查item["name"]但在此之前还有一个名为"node"的键。 For this reason you were getting error.出于这个原因,您遇到了错误。

As if you return only item inside loop as below,好像您只返回循环内的item ,如下所示,

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

you will get你会得到

{'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}}

So to get your required answer you have to do as below:因此,要获得所需的答案,您必须执行以下操作:

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

which will give you output as这会给你 output 作为

{'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}

you have first to access to the key "node"您必须首先访问关键“节点”

You may do it this way:你可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM