简体   繁体   English

从嵌套字典中提取匹配值

[英]Extracting matching values from nested dictionary

I'm trying to extract values from a nested dictionary if a value matches the value in a list.如果值与列表中的值匹配,我正在尝试从嵌套字典中提取值。

data = [
            {
                "id": 12345678,
                "list_id": 12345,
                "creator_id": 1234567,
                "entity_id": 1234567,
                "created_at": "2020-01-30T00:43:55.256-08:00",
                "entity": {
                    "id": 123456,
                    "type": 0,
                    "first_name": "John",
                    "last_name": "Doe",
                    "primary_email": "john@fakemail.com",
                    "emails": [
                        "john@fakemail.com"
                    ]
                }
            },
            {
                "id": 12345678,
                "list_id": 12345,
                "creator_id": 1234567,
                "entity_id": 1234567,
                "created_at": "2020-01-30T00:41:54.375-08:00",
                "entity": {
                    "id": 123456,
                    "type": 0,
                    "first_name": "Jane",
                    "last_name": "Doe",
                    "primary_email": "jane@fakemail.com",
                    "emails": [
                        "jane@fakemail.com"
                    ]
                }
            }
        ]

The code is as follows.代码如下。

match_list = ['jane@fakemail.com',[]]
first_names = []
email = []
for i in match_list:
    for record in data:
        if 'primary_email' == i:
            email.append(record.get('entity',{}).get('primary_email', None))
            first_names.append(record.get('entity',{}).get('first_name', None))       
print(first_names)
print(email)

Instead of returning the matching values this only returns empty lists.而不是返回匹配的值,这只会返回空列表。 Any help here would be much appreciated.任何帮助在这里将不胜感激。

The expected output is预期的输出是

first_names = ['Jane'] and email = ['jane@fakemail.com']

Store temporary values in variables, to make your code easier to handle:将临时值存储在变量中,以使您的代码更易于处理:

emails = []
names = []
match_list = ['jane@fakemail.com',[]]


for item in data:
    entry = item.get('entity', {})

    fName = entry.get('first_name', '')
    pMail = entry.get('primary_email', '')

    if pMail in match_list:
        print (fName)
        print (pMail)

        emails.append(pMail)
        names.append(fName)

Output:输出:

Jane
jane@fakemail.com

In the 6th line of your code在代码的第 6 行

    if 'primary_email' == i:

You're comparing elements from match_list (that is 'i') to literally the string called 'primary_email' instead of the actual email.您将 match_list(即 'i')中的元素与字面上名为 'primary_email' 的字符串而不是实际的电子邮件进行比较。 since 'jane@fakemail.com' is not equal to 'primary_email' (literally the string).因为 'jane@fakemail.com' 不等于 'primary_email'(字面意思是字符串)。

Instead use而是使用

if record['entity']['primary_email'] == i:

and your code should work as expected.并且您的代码应该按预期工作。

In your code you would always get an empty list as you are comparing 'primary_email'==i which will always be False .在您的代码中,当您比较'primary_email'==i ,您总是会得到一个空列表,该列表将始终为False

Change it to record['entity']['primary_email']==i .将其更改为record['entity']['primary_email']==i

And here there is no need to use get .Since if mail doesn't match with any of the primary_email then nothing happens.这里没有必要使用get 。因为如果mail与任何primary_email都不匹配,那么什么都不会发生。 primary_email will only be added when it meets the condition d['entity']['primary_email']==mail . primary_email只有在满足条件d['entity']['primary_email']==mail时才会添加。

Try this I refactored your code little bit.试试这个 我稍微重构了你的代码。

In [25]: for mail in match_list:
    ...:     for d in data:
    ...:         if d['entity']['primary_email']==mail:
    ...:             first_name.append(d['entity']['first_name'])
    ...:             emails.append(d['entity']['primary_email'])

output输出

In [26]: emails
Out[26]: ['jane@fakemail.com']

In [27]: first_name
Out[27]: ['Jane']

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

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