【发布时间】:2022-11-21 19:39:55
【问题描述】:
我想编写一个函数来检查 dict1(基本字典)的键并将其与 dict2(嵌套字典列表,可以是一个或多个)的键进行比较,以便它检查强制键,然后检查可选键(如果和无论存在什么)并将差异作为列表返回。
dict1 = {"name": str, #mandatory
"details" : { #optional
"class" : str, #optional
"subjects" : { #optional
"english" : bool, #optional
"maths" : bool #optional
}
}}
dict2 = [{"name": "SK",
"details" : {
"class" : "A"}
},
{"name": "SK",
"details" : {
"class" : "A",
"subjects" :{
"english" : True,
"science" : False
}
}}]
将 dict2 与 dict1 进行比较后,预期输出为:-
pass #no difference in keys in 1st dictionary
["science"] #the different key in second dictionary of dict2
【问题讨论】:
标签: python list dictionary nested comparison