[英]Python 3: Show Differences Between Key-Value Pairs of Dictionaries
I'd like to find the differences between two dictionaries that have multiple values for a key.我想找到两个具有多个键值的字典之间的差异。 All the examples that I find, the dictionaries have a key which contains only one value.我找到的所有例子,字典都有一个只包含一个值的键。 Say you have keys that have multiple values as in the following example:假设您的键具有多个值,如下例所示:
pizza_1 = {"toppings": ["cheese", "pepperoni", "mushroom"],
"crust": ["deep dish", "hand tossed", "thin"],
"size": ["large", "medium", "small"],
"price": ["$12.99", "$9.99", "$7.99"]}
pizza_2 = {"toppings": ["cheese", "pepperoni", "olive"],
"crust": ["deep dish", "traditional", "thin"],
"size": ["large", "medium", "small"],
"brand": ["Domino's", "Pizza Hut", "Little Caesars"]}
I want to return only the differences in the two dictionaries including both the key and the values.我只想返回两个字典中的差异,包括键和值。 Either the difference in pizza_1 or pizza_2, it does not matter which dictionary.无论是pizza_1 还是pizza_2 的不同,哪个字典都没有关系。 Example of what I'm looking for below:我在下面寻找的示例:
print(differences)
"toppings": ["mushroom"]
"crust": ["hand tossed"]
"price": ["$12.99", "$9.99", "$7.99"]
I'm not sure exactly how it will output, but wanted to give an example of what I am sort of looking for.我不确定它将如何输出,但想举一个我正在寻找的例子。 Thanks in advance for taking your time to help!提前感谢您抽出宝贵时间提供帮助!
Loop over the union of keys, treat the values as sets and print the set difference :循环键的并集,将值视为集合并打印 集合差异:
for key in pizza_1.keys() | pizza_2: # union of the dict views
difference = set(pizza_1.get(key, [])).difference(pizza_2.get(key, []))
if difference:
print(key, list(difference))
I'm using the dict.keys()
dictionary view here to provide the union of dictionary keys.我在这里使用dict.keys()
字典视图来提供字典键的联合。 The if
test filters out empty results. if
测试过滤掉空结果。
If you wanted this as a dictionary, you can produce one with a generator expression plus a dict comprehension to avoid producing the sets more than once:如果你想把它作为一个字典,你可以用一个生成器表达式加上一个字典理解来生成一个字典,以避免多次生成集合:
differences = ((key, list(set(pizza_1.get(key, [])).difference(pizza_2.get(key, []))))
for key in pizza_1.keys() | pizza_2)
differences = {k: v for k, v in differences if v}
Demo:演示:
>>> for key in pizza_1.keys() | pizza_2: # union of the dict views
... difference = set(pizza_1.get(key, [])).difference(pizza_2.get(key, []))
... if difference:
... print(key, list(difference))
...
crust ['hand tossed']
toppings ['mushroom']
price ['$9.99', '$7.99', '$12.99']
>>> differences = ((key, list(set(pizza_1.get(key, [])).difference(pizza_2.get(key, []))))
... for key in pizza_1.keys() | pizza_2)
>>> {k: v for k, v in differences if v}
{'crust': ['hand tossed'], 'toppings': ['mushroom'], 'price': ['$9.99', '$7.99', '$12.99']}
I propose a dictionary with set-values as your output-datastructure.我建议使用设置值作为输出数据结构的字典。
>>> {k: set(v).difference(pizza_2.get(k, {})) for k, v in pizza_1.items()}
{'price': {'$9.99', '$7.99', '$12.99'}, 'size': set(), 'toppings': {'mushroom'}, 'crust': {'hand tossed'}}
The result will hold an empty set for a key where there's no difference (see size).结果将为没有差异的键保留一个空集(请参阅大小)。
What I did was loop through keys and use list comprehensions to get the difference (specifically what's in pizza_1
that's not in pizza_2
as your example output shows).我所做的是遍历键并使用列表pizza_1
来获得差异(特别是pizza_1
中的内容, pizza_2
如示例输出所示, pizza_1
中没有的pizza_2
)。
def getDiff(dict1, dict2):
diff = {}
for key in dict1:
if key not in dict2:
diff[key] = dict1[key]
elif dict1[key] != dict2[key]:
diff[key] = [e for e in dict1[key] if e not in dict2[key]]
return diff
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.