如何在Python中比较两个不同字典的键?
我有两个字典,我想比较对应键的值。例如,如果我有如何在Python中比较两个不同字典的键?,python,python-2.7,dictionary,Python,Python 2.7,Dictionary,我有两个字典,我想比较对应键的值。例如,如果我有 dict1 = {'a':1, 'b':0, 'c':3} dict2 = {'a':0, 'b':0, 'c':4} 然后返回False,因为dict2的对应值不能大于dict11(在dict2中,'a'的值小于dict1的值是可以的,但是dict2中的'c'的值大于dict1的值是不可以的) 此外,如果dict2的值未在dict1中列出,则不允许使用此选项。例如: dict1 = {'a':1, 'b':0, 'c':3} dict2 =
dict1 = {'a':1, 'b':0, 'c':3}
dict2 = {'a':0, 'b':0, 'c':4}
然后返回False
,因为dict2
的对应值不能大于dict11
(在dict2
中,'a'
的值小于dict1
的值是可以的,但是dict2
中的'c'
的值大于dict1
的值是不可以的)
此外,如果dict2
的值未在dict1
中列出,则不允许使用此选项。例如:
dict1 = {'a':1, 'b':0, 'c':3}
dict2 = {'a':0, 'b':0, 'd':2}
(但如果dict1
具有dict2
不具有的值,则可以)。换句话说,dict2
必须是dict1
的一个子集,包括键和值
一旦代码捕捉到其中一个违规行为,我想立即停止一切运行,并返回False
这就是我所尝试的:
condition = True #True by default
for letter in dict2:
if dict2[letter] > dict1[letter] or dict1[letter] == None:
condition = False
break
break
但是当我遇到一个列在dict1
而不是dict2
中的键时,我会得到一个KeyError
如何修复此问题?我想您正在寻找类似的解决方案:
condition = all(k in dict1 and dict2[k] <= dict1[k] for k in dict2)
condition=all(dict1和dict2中的k[k]如果“常规”dict:
如果找不到密钥,它只会返回false,并且不会抛出错误。尝试修改
condition = True #True by default
for letter in dict2:
if letter not in dict1.keys() or dict2[letter] > dict1[letter]:
condition = False
break
break
这里有一个避免使用try-except的简单方法。我还包括了几个测试用例,所有的比较都在compare_2lt1中
# Set 1 violates the comparison in key 'c', too large
dict1_1 = {'a':1, 'b':0, 'c':3}
dict2_1 = {'a':0, 'b':0, 'c':4}
# Set 2 violates the comparison in key 'd', doesn't exist
dict1_2 = {'a':1, 'b':0, 'c':3}
dict2_2 = {'a':0, 'b':0, 'c':2, 'd':5}
# Set 3 is True
dict1_3 = {'a':1, 'b':0, 'c':3}
dict2_3 = {'a':0, 'b':0, 'c':2}
def compare_2lt1(d1, d2):
for key in d2:
if key in d1 and d1[key] >= d2[key]:
continue
else:
return False
return True
def test(d1, d2):
print d1
print d2
print compare_2lt1(d1, d2)
print "-----"
test(dict1_1, dict2_1)
test(dict1_2, dict2_2)
test(dict1_3, dict2_3)
输出:
{'a': 1, 'c': 3, 'b': 0}
{'a': 0, 'c': 4, 'b': 0}
False
-----
{'a': 1, 'c': 3, 'b': 0}
{'a': 0, 'c': 2, 'b': 0, 'd': 5}
False
-----
{'a': 1, 'c': 3, 'b': 0}
{'a': 0, 'c': 2, 'b': 0}
True
-----
您得到的是keyError,因为没有为字典中不存在的键映射的值,因此它将给出keyError,而不是在检查内部循环时将未初始化的键值作为“None”:
if dict2[letter] > dict1[letter] or dict1[letter] == None:
@邓肯写了一个很好的单线理解答案,
我只是想在你尝试的时候重写它
d1 = { 'a' = 1,'b' = 2, 'c'= 3}
d2 = { 'a' = 2,'b' = 3, 'd'= 4}
键“d”在d1中不存在,因此应返回False
现在,当您查找条件时,它的值将为False,这是您想要的吗
#Value in dict2 greater than value in dict1 for same key ?
condition1 = sum([dict2[a] > dict1[a] for a in dict2 if a in dict1]) == 0
#keys in dict2 not in dict1
condition2 = [a for a in dict2 if a not in dict1] == []
#The two conditions
condition = condition1 and condition2
#Test
if condition:
print True
print False
使用尝试除
语句之外的其他语句。您如何阅读此语句?这就像列表理解。我总是很难弄清楚它到底在做什么。
#Value in dict2 greater than value in dict1 for same key ?
condition1 = sum([dict2[a] > dict1[a] for a in dict2 if a in dict1]) == 0
#keys in dict2 not in dict1
condition2 = [a for a in dict2 if a not in dict1] == []
#The two conditions
condition = condition1 and condition2
#Test
if condition:
print True
print False