简体   繁体   English

Python 在嵌套字典中搜索特定值并获取包含该值的字典

[英]Python Search for a specific value in a nested dictionary and get one dictionary including the value

I am trying to get a dictionary that includes a specific value from nested dictionaries.我正在尝试从嵌套字典中获取包含特定值的字典。

My multiple dictionaries look like this:我的多个字典如下所示:

MyDto = [{'app' : 'chrome',
'id': 'adetwt',
'name': 'John',
'selection': {'food':1324, 'var1': 2039, 'var2': 2126, 'var3': 0}},
{'app' : 'chrome',
'id': 'asdfb',
'name': 'Sam',
'selection': {'food':1324, 'var1': 2011, 'var2': 2123, 'var3': 0}},
{'app' : 'chrome',
'id': 'sfsf',
'name': 'Mary',
'selection': {'food':1324, 'var1': 2013, 'var2': 2111, 'var3': 0}}]

I want to only get a dictionary with a specific value.我只想获得具有特定值的字典。 For example, in this example,例如,在这个例子中,

if MyDto['id'] = 'sfsf'

The result I want to get is我想要得到的结果是

extracted_dict = {'app' : 'chrome',
'id': 'sfsf',
'name': 'Mary',
'selection': {'food':1324, 'var1': 2013, 'var2': 2111, 'var3': 0}

Is there a way to get a dictionary based on a specific value it includes?有没有办法根据它包含的特定值来获取字典? Sorry if this is too basic question:( Thanks a lot!对不起,如果这是太基本的问题:(非常感谢!

Just iterate over items and get the right dict as below:只需遍历项目并获得正确的字典,如下所示:

MyDto = [
    {'app': 'chrome',
     'id': 'adetwt',
     'name': 'John',
     'selection': {'food': 1324, 'var1': 2039, 'var2': 2126, 'var3': 0}},
    {'app': 'chrome',
     'id': 'asdfb',
     'name': 'Sam',
     'selection': {'food': 1324, 'var1': 2011, 'var2': 2123, 'var3': 0}},
    {'app': 'chrome',
     'id': 'sfsf',
     'name': 'Mary',
     'selection': {'food': 1324, 'var1': 2013, 'var2': 2111, 'var3': 0}}
]


def get_by_id(id: str):
    for dict_ in MyDto:
        if dict_['id'] == id:
            return dict_['selection']


print(get_by_id('sfsf'))

I would go with something like:我会 go 类似:

key,value = 'id','sfsf'
dictList = [ myDict for myDict in MyDto if myDict.get(key) == value ]
print(dictList)

output output

[{'app': 'chrome', 'id': 'sfsf', 'name': 'Mary', 'selection': {'food': 1324, 'var1': 2013, 'var2': 2111, 'var3': 0}}]

The list comprehension obviously produces a list of dict and not a single dict.列表理解显然会产生一个字典列表而不是单个字典。 With this list comprehension you scan all the dicts in your MyDto , and select only the ones which contain the pair key/value you chose.使用此列表理解,您可以扫描MyDto中的所有字典,而 select 仅扫描包含您选择的键/值对的字典。 It also works if the key is not present in any dictionary in MyDto , avoiding a KeyError exception, producing and empty list.如果密钥在MyDto的任何字典中都不存在,它也可以工作,避免KeyError异常,产生和空列表。

If you are sure that at most only one dict with key/value is contained in the list:如果您确定列表中最多只包含一个带有键/值的字典:

def getByKeyAndValue(dictList, key, value):
        for myDict in MyDto:
            if myDict.get(key) == value:
                return myDict
        return {}

extracted_dict = getByKeyAndValue(MyDto, 'id', 'sfsf')

This function scans your MyDto only until it find the dict you want to extract.MyDto仅扫描您的 MyDto,直到找到您要提取的 dict。 If it does not find the dict, it returns an empty dict.如果没有找到字典,则返回一个空字典。

gotta love list comprehension:必须爱列表理解:

[d for d in MyDto if d['id'] == 'sfsf']

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

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