[英]How to compare two dictionary elements which has list of dicts using 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
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)
您可以安裝一個名為 deepdiff 的模塊
pip 安裝 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))
答案非常簡潔,有條理,而且速度很快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.