如何使用python比较包含dict列表的两个dictionary元素

2024-09-29 23:32:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图比较两个字典,其中包含作为字典元素列表的值。如果字典相同,我的结果应该返回True或False,否则返回False,如果字典不同,我希望得到不同的值。下面是我尝试过的代码,我尝试过引用更多的stackoverflow答案,但无法从中获得。因此,发布了一个新问题。我也是python新手

下面是我的示例数据

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

Tags: nameincomidhellofor字典dict
2条回答

您可以安装名为deepdiff的模块

pip install deepdiff

您需要的代码如下所示:

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

答案非常简洁、有条理,而且速度很快

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

如果键未排序

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)

相关问题 更多 >

    热门问题