回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>如果值与列表中的值匹配,我将尝试从嵌套字典中提取值</p>
<pre><code>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"
]
}
}
]
</code></pre>
<p>代码如下</p>
<pre><code>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)
</code></pre>
<p>它不返回匹配值,只返回空列表。这里的任何帮助都将不胜感激</p>
<p>预期产量为</p>
<pre><code>first_names = ['Jane'] and email = ['jane@fakemail.com']
</code></pre>