我有两个字典,它们有相同的键和值对。我只想比较特定的键值对并返回true。我是python的新手,请帮我写一个同样的函数。
这些字典是
A: {'id1': 'target', 'start1': '39', 'end1': '45', \
'id2': 'query', 'start2': '98', 'end2': '104'}
B: {'id1': 'target', 'start1': '39', 'end1': '45', \
'id2': 'query', 'start2': '98', 'end2': '104'}这里我想检查'start1'、'end1'、'start2'和'end2'的值是否相同。
发布于 2020-02-19 20:42:04
result = all( A[k]==B[k] for k in ('start1', 'end1', 'start2', 'end2'))发布于 2020-02-19 21:19:44
您可以使用for循环:
wanted_keys = {'start1', 'end1', 'start2', 'end2'}
same = True
for k in wanted_keys:
if A.get(k) != B.get(k):
same = False
break 一行代码:
all(A.get(k) == B.get(k) for k in wanted_keys)https://stackoverflow.com/questions/60300582
复制相似问题