Python - 两个(稍有不同)JSON数组比较

时间:2018-05-27 08:40:41

标签: python python-2.7

我需要比较下面的两个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个项目。

2 个答案:

答案 0 :(得分:2)

只需使用简单的帮助函数

bbox (201, 18, 200, 20)

答案 1 :(得分:0)

@Danny ,您还可以使用Python的列表理解 map() filter() lambda函数等,如下面的代码示例所示。

如果您在阅读时遇到任何问题,请发表评论。理解我的代码。

  

检查Lambda, filter(), map()List comprehension。在编程中使用这些概念真的很棒。

1行代码

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))

现在逐一查看下面的代码示例。

示例1

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'}]

示例2

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"
    }
]
"""