我需要比较下面的两个 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_all 中删除所有 emails_exist 值,我只想构建一个列表来显示未出现在 中的电子邮件的 email/first_name >emails_exist ...类似于下面的emails_new
输出
emails_new = [{'email': 'new@email.com', 'first_name': 'New'}]
可能值得注意:这些 JSON 数组每个最多可能有 100 个项目。
请您参考如下方法:
只需使用一个简单的辅助函数
def contains(emails, email):
for contained_email in emails:
if email["email"] == contained_email["email"]:
return True
return False
emails_new = [email for email in emails_all if not contains(emails_exist, email)]
