简体   繁体   中英

How to compare two dictionary elements which has list of dicts using python

I'm trying to compare two dictionaries which contains values as list of dictionary elements. My result should return True or False if the dictionary is same else False, also if the dictionary is different, i would like to get the value which is different.Following is the code i have tried, i have tried to refer some more stackoverflow answers but I'm unable to get from them.Hence posting a new question.I'm new to python as well.

following is my sample data

dict_a = {
    "hello@gmail.com": [
        {
            "casecode": "143-10",
            "ServiceName": "ec2",
            "ID/TypeOfService": "instance/i-030e7c1f50e06a500",
            "ipaddress": "172.21.156.26",
            "intance_name": "test-demo",
            "TechnicalOwner": "hello@gmail.com",
            "Owner": "Empty"
        },
        {
            "casecode": "243-11",
            "ServiceName": "s3",
            "ID/TypeOfService": "s3-for-logs",
            "ipaddress": "Empty",
            "intance_name": "Empty",
            "TechnicalOwner": "hello@gmail.com",
            "Owner": "Empty"
        }
    ]
}


dict_b = {
    "hello@gmail.com": [
        {
            "casecode": "143-10",
            "ServiceName": "ec2",
            "ID/TypeOfService": "instance/i-030e7c1f50e06a500",
            "ipaddress": "172.21.156.26",
            "intance_name": "test-demo",
            "TechnicalOwner": "hello@gmail.com",
            "Owner": "Empty"
        },
        {
            "casecode": "243-10",
            "ServiceName": "s3",
            "ID/TypeOfService": "s3-for-logs",
            "ipaddress": "Empty",
            "intance_name": "Empty",
            "TechnicalOwner": "hello@gmail.com",
            "Owner": "Empty"
        }
    ]
}

def get_lists(dict_a,dict_b):
    for type_, case_code_info in dict_a.items():
        dict_b_case_code = dict_a[type_]
        if type_ in dict_b.keys():
            for item_a in case_code_info:
                for item_b in dict_b_case_code:
                    value_b = item_b.values()
                    for (key_a,value_a) in item_a.items():
                        if value_a == value_b:
                            return True
                        else:
                            return False
same = True

for (x,x_nest),(y,y_nest) in zip(dict_a.items(),dict_b.items()):
    for x_nested, y_nested in zip(x_nest, y_nest):
        for(x_key,x_value),(y_key,y_value) in zip(x_nested.items(),y_nested.items()):
            if x_value != y_value:
                print(x_key,x_value)
                print(y_key,y_value,'\n')
                same = False
            else:
                pass
print(same)

>>> casecode 243-11
    casecode 243-10 

    False

If Keys aren't sorted

same = True

for (x,x_nest),(y,y_nest) in zip(dict_a.items(),dict_b.items()):
    for x_nested, y_nested in zip(x_nest, y_nest):
        for x_key,x_value in x_nested.items():
            for y_key,y_value in y_nested.items():
                if y_key == x_key:
                    if x_value != y_value:
                        print(x_key,x_value)
                        print(y_key,y_value,'\n')
                        same = False
                    else:
                        pass
print(same)

You can install a module named deepdiff

pip install deepdiff

The code you want looks like:

dict_1 = {
    "hello@gmail.com": [
        {
            "casecode": "143-10",
            "ServiceName": "ec2",
            "ID/TypeOfService": "instance/i-030e7c1f50e06a500",
            "ipaddress": "172.21.156.26",
            "intance_name": "test-demo",
            "TechnicalOwner": "hello@gmail.com",
            "Owner": "Empty"
        },
        {
            "casecode": "243-11",
            "ServiceName": "s3",
            "ID/TypeOfService": "s3-for-logs",
            "ipaddress": "Empty",
            "intance_name": "Empty",
            "TechnicalOwner": "hello@gmail.com",
            "Owner": "Empty"
        }
    ]
}


dict_2 = {
    "hello@gmail.com": [
        {
            "casecode": "143-10",
            "ServiceName": "ec2",
            "ID/TypeOfService": "instance/i-030e7c1f50e06a500",
            "ipaddress": "172.21.156.26",
            "intance_name": "test-demo",
            "TechnicalOwner": "hello@gmail.com",
            "Owner": "Empty"
        },
        {
            "casecode": "243-10",
            "ServiceName": "s3",
            "ID/TypeOfService": "s3-for-logs",
            "ipaddress": "Empty",
            "intance_name": "Empty",
            "TechnicalOwner": "hello@gmail.com",
            "Owner": "Empty"
        }
    ]
}
import deepdiff
import json

diff = deepdiff.DeepDiff(dict_1, dict_2)
print(json.dumps(diff, indent=4))

The answer is pretty neat and organized as well as fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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