[英]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.
如果字典相同,我的结果应该返回 True 或 False,否则返回 False,如果字典不同,我想获得不同的值。以下是我尝试过的代码,我尝试参考更多 stackoverflow 答案但我无法从他们那里得到。因此发布了一个新问题。我也是 python 的新手。
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您可以安装一个名为 deepdiff 的模块
pip install deepdiff
pip 安装 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.答案非常简洁,有条理,而且速度很快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.