我试图比较两个字典,其中包含作为字典元素列表的值。如果字典相同,我的结果应该返回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
您可以安装名为deepdiff的模块
您需要的代码如下所示:
答案非常简洁、有条理,而且速度很快
如果键未排序
相关问题 更多 >
编程相关推荐