I am new to Python. I have two dictionaries which share the same keys but different values for the keys. I would like to compare the two dictionaries so that I would get the numerical difference of the values for each key. For example:
我是Python的新手。我有两个字典共享相同的键但键的值不同。我想比较两个词典,以便得到每个键的值的数值差异。例如:
dict1 = {'hi' : 45, 'thanks' : 34, 'please' : 60}
dict1 = {'hi':45,'thanks':34,'please':60}
dict2 = {'hi' : 40, 'thanks' : 46, 'please' : 50}
dict2 = {'hi':40,'thanks':46,'please':50}
In other words, I would like to receive a third dictionary or a list of pairs that would show the numerical difference of the values within these two dictionaries (i.e.subtracting the values of dict1 from dict2 (or vice versa). It would thus be something like this:
换句话说,我想收到第三本字典或一对列表,它们将显示这两个字典中值的数值差异(从dict2中减去dict1的值(反之亦然)。喜欢这个:
dict_difference = {'hi' : 5 , 'thanks' : -12, 'please' : 10}
dict_difference = {'hi':5,'thanks': - 12,'please':10}
I know that subtracting one dictionary from another by :
我知道从另一个字典中减去一个字典:
dict1 = Counter({'hi' = 45, 'thanks' = 34, 'please' = 60})
dict1 = Counter({'hi'= 45,'thanks'= 34,'please'= 60})
dict2 = Counter({'hi' = 40, 'thanks' = 46, 'please' = 50})
dict2 = Counter({'hi'= 40,'thanks'= 46,'please'= 50})
dict3 = dict1-dict2
# will only return the positive values so it will give:
dict3 = dict1-dict2#只返回正值,因此它会给出:
dict3 = {'hi'= 5, 'please' = 10}
# which is NOT what I want.
dict3 = {'hi'= 5,'please'= 10}#这不是我想要的。
I also thought of converting the dictionaries into a list of pairs (I think this is how it is called) :
我还想过将字典转换成一对列表(我认为这就是它的调用方式):
dictlist = []
for key, value in dict1.iteritems():`
temp = (key, value)
dictlist.append(temp)
and therefore
因此
print dictlist #gives:
[('hi', 45),('thanks' = 34), ('please' = 60)]
So I thought that if I can manage to convert the dictionary into the list of pairs and then to make it in the form of the key:value to be key = value I would be able to apply the subtract() method and achieve what I want.
所以我想如果我可以设法将字典转换为对的列表然后以键的形式使它:值为key = value我将能够应用subtract()方法并实现我的目标想。
I thought of achieving it through the def __repr__(self)
as shown in https://docs.python.org/2/library/collections.html but I didn't get far.
我想通过def __repr __(self)实现它,如https://docs.python.org/2/library/collections.html所示,但我没有走得太远。
I would be most grateful for any help. Please, if possible leave description for your code. If my approach is wrong and there is an easier way of subtracting the values of one dictionary from another please share it as well.
我会非常感谢任何帮助。如果可能,请留下您的代码说明。如果我的方法是错误的,并且有一种更简单的方法从另一个字典中减去一个字典的值,请分享它。
2 个解决方案
#1
9
First, the format of your dictionaries is not correct (:
and not =
):
首先,你的词典格式不正确(:而不是=):
dict1 = {'hi':45, 'thanks':34, 'please':60}
dict2 = {'hi':40, 'thanks':46, 'please':50}
You can use a dictionary comprehension. You basically loop through one dictionary, check that the key is also in the second dictionary and insert the difference of the values in the output dictionary. All in one line.
您可以使用字典理解。您基本上循环遍历一个字典,检查该键是否也在第二个字典中,并在输出字典中插入值的差异。全部在一条线上。
dic = {key: dict1[key]-dict2[key] for key in dict1 if key in dict2}
#2
3
You were on the right path with thinking about using the dictionarys' keys.
考虑使用dictionarys的键,你走在正确的道路上。
Here, I go through the first dictionary's keys, checking if they're in dictionary2. Then I do the same with dictionary2, checking for keys in dictionary1, but also ensuring that the key isn't already in the result dictionary so we don't do duplicate subtraction.
在这里,我查看第一个字典的键,检查它们是否在字典2中。然后我对dictionary2做了同样的事情,检查了dictionary1中的键,但也确保键不在结果字典中,所以我们不做重复的减法。
dict1 = {'hi': 45, 'thanks': 34, 'please': 60}
dict2 = {'hi': 40, 'thanks': 46, 'please': 50}
result = {}
for key in dict1.keys():
if key in dict2:
result[key] = dict1[key] - dict2[key]
for key in dict2.keys():
if key in dict1 and not key in result:
result[key] = dict1[key] - dict2[key]
#1
9
First, the format of your dictionaries is not correct (:
and not =
):
首先,你的词典格式不正确(:而不是=):
dict1 = {'hi':45, 'thanks':34, 'please':60}
dict2 = {'hi':40, 'thanks':46, 'please':50}
You can use a dictionary comprehension. You basically loop through one dictionary, check that the key is also in the second dictionary and insert the difference of the values in the output dictionary. All in one line.
您可以使用字典理解。您基本上循环遍历一个字典,检查该键是否也在第二个字典中,并在输出字典中插入值的差异。全部在一条线上。
dic = {key: dict1[key]-dict2[key] for key in dict1 if key in dict2}
#2
3
You were on the right path with thinking about using the dictionarys' keys.
考虑使用dictionarys的键,你走在正确的道路上。
Here, I go through the first dictionary's keys, checking if they're in dictionary2. Then I do the same with dictionary2, checking for keys in dictionary1, but also ensuring that the key isn't already in the result dictionary so we don't do duplicate subtraction.
在这里,我查看第一个字典的键,检查它们是否在字典2中。然后我对dictionary2做了同样的事情,检查了dictionary1中的键,但也确保键不在结果字典中,所以我们不做重复的减法。
dict1 = {'hi': 45, 'thanks': 34, 'please': 60}
dict2 = {'hi': 40, 'thanks': 46, 'please': 50}
result = {}
for key in dict1.keys():
if key in dict2:
result[key] = dict1[key] - dict2[key]
for key in dict2.keys():
if key in dict1 and not key in result:
result[key] = dict1[key] - dict2[key]