我需要比较下面的两个JSON数组,并最终得到下面的emails_new
结果......我发现的泥浆中的一个是一个数组有一个额外的键/值对,我需要在更新的数组中。所以很难匹配。
本质; '如果emails_exist中的任何电子邮件出现在emails_all中,请不要将它们包含在emails_new中
emails_all = [{'email': 'new@email.com', 'first_name': 'New Name'}, {'email': 'exists@email.com', 'first_name': 'Exists Name'}]
emails_exist = [{'email': 'exists@email.com'}]
从emails_exist
中删除所有emails_all
值,我只想构建一个列表,以显示emails_exist
中未显示的电子邮件的电子邮件/ first_name ...与下面的emails_new
emails_new = [{'email': 'new@email.com', 'first_name': 'New'}]
可能值得注意:这些JSON数组每个最多可包含100个项目。
答案 0 :(得分:2)
只需使用简单的帮助函数
bbox (201, 18, 200, 20)
答案 1 :(得分:0)
@Danny ,您还可以使用Python的列表理解, map(), filter(), lambda函数等,如下面的代码示例所示。
如果您在阅读时遇到任何问题,请发表评论。理解我的代码。
检查Lambda, filter(), map()和List comprehension。在编程中使用这些概念真的很棒。
emails_new = list(filter(lambda email_all_dict: True if sum(list(map(lambda email_exist_dict: True if email_exist_dict["email"] == email_all_dict['email'] else False, emails_exist))) == 0 else False, emails_all))
现在逐一查看下面的代码示例。
emails_all = [{'email': 'new@email.com', 'first_name': 'New Name'}, {'email': 'exists@email.com', 'first_name': 'Exists Name'}]
emails_exist = [{'email': 'exists@email.com'}]
emails_new = list(filter(lambda email_all_dict: True if sum(list(map(lambda email_exist_dict: True if email_exist_dict["email"] == email_all_dict['email'] else False, emails_exist))) == 0 else False, emails_all))
print(emails_new)
# [{'first_name': 'New Name', 'email': 'new@email.com'}]
emails_all = [
{'email': 'new1@email.com', 'first_name': 'New Name1'},
{'email': 'exists1@email.com', 'first_name': 'Exists Name1'},
{'email': 'exists2@email.com', 'first_name': 'Exists Name2'},
{'email': 'exists5@email.com', 'first_name': 'Exists Name5'},
{'email': 'exists6@email.com', 'first_name': 'Exists Name6'},
{'email': 'new9@email.com', 'first_name': 'New Name9'},
{'email': 'exists7@email.com', 'first_name': 'Exists Name7'},
{'email': 'exists8@email.com', 'first_name': 'Exists Name8'},
{'email': 'exists4@email.com', 'first_name': 'Exists Name4'},
{'email': 'new4@email.com', 'first_name': 'New Name4'},
{'email': 'new2@email.com', 'first_name': 'New Name2'},
{'email': 'exists3@email.com', 'first_name': 'Exists Name3'}
]
emails_exist = [
{'email': 'exists@email.com'}, # Does not exist
{'email': 'exists7@email.com', 'first_name': 'Exists Name7'},
{'email': 'exists8@email.com', 'first_name': 'Exists Name8'},
{'email': 'exists4@email.com', 'first_name': 'Exists Name4'}
]
emails_new2 = list(filter(lambda email_all_dict: True if sum(list(map(lambda email_exist_dict: True if email_exist_dict["email"] == email_all_dict['email'] else False, emails_exist))) == 0 else False, emails_all))
# Pretty printing list
print(json.dumps(emails_new2, indent=4))
"""
[
{
"first_name": "New Name1",
"email": "new1@email.com"
},
{
"first_name": "Exists Name1",
"email": "exists1@email.com"
},
{
"first_name": "Exists Name2",
"email": "exists2@email.com"
},
{
"first_name": "Exists Name5",
"email": "exists5@email.com"
},
{
"first_name": "Exists Name6",
"email": "exists6@email.com"
},
{
"first_name": "New Name9",
"email": "new9@email.com"
},
{
"first_name": "New Name4",
"email": "new4@email.com"
},
{
"first_name": "New Name2",
"email": "new2@email.com"
},
{
"first_name": "Exists Name3",
"email": "exists3@email.com"
}
]
"""