{'object-1301': {'act1': 'false',
'act1x': 'true',
'act': 'false',
'actAdv': 'false',
'actAe': 'false',
'actArp': 'false',},
'object-218': {'act1': 'true',
'act1x': 'false',
'act': 'false',
'actAdv': 'false',
'actAe': 'true',
'actArt': 'false',},
...
}
到目前为止,代码I是,其中ref是引用对象,dict是我在上面复制的字典:
main_dif = {}
for key, value in dict.iteritems():
dif_k = {}
for k, v in value.iteritems():
try:
a = ref[k]
except:
a = None
if a != v:
dif_k.update({k:v})
main_dif.update({key:dif_k})
我接下来的问题是:当参考对象没有任何参数,但是其他一些对象有,这很有效,但是当参考对象有和其他对象没有时,有没有任何事情发生,无法找到它。
答案 0 :(得分:1)
这是一个更详细的答案。看看这是否适合您检查键和值。
from pprint import pprint
# Use your own ref, I've just this one all to true for testing.
ref = {
'act1': 'true',
'act1x': 'true',
'act': 'true',
'actAdv': 'true',
'actAe': 'true',
'actArp': 'true',
}
data = {
'object-1301': {
'act1': 'false',
'act1x': 'true',
'act': 'false',
'actAdv': 'false',
'actAe': 'false',
'actArp': 'false',
},
'object-218': {
'act1': 'true',
'act1x': 'false',
'act': 'false',
'actAdv': 'false',
'actAe': 'true',
'actArt': 'false',
},
'object-short': {
'act1': 'false'
},
'object-extra-keys': {
'act1': 'true',
'act1x': 'true',
'act': 'true',
'actAdv': 'true',
'actAe': 'true',
'actArp': 'true',
'extra': 'true',
},
'object-as-ref': {
'act1': 'true',
'act1x': 'true',
'act': 'true',
'actAdv': 'true',
'actAe': 'true',
'actArp': 'true',
},
'object-opposite-to-ref': {
'act1': 'false',
'act1x': 'false',
'act': 'false',
'actAdv': 'false',
'actAe': 'false',
'actArp': 'false',
}
}
mainDiff = {}
for obj, objDict in data.iteritems():
currentDict = {}
# Check if the keys match.
if objDict.keys() != ref.keys():
for key in ref:
if key not in objDict.keys():
# Store keys missing from object.
currentDict[key] = None
for key in objDict:
if key not in ref.keys():
# Store unexpected values in object.
currentDict[key] = None
# Check if values are the same.
for k, v in objDict.iteritems():
# Check if the key is in ref to avoid error
if k in ref:
# Then compare if values are equal
if ref[k] != objDict[k]:
# Then store actual value
currentDict[k] = v
# Store differences for current object against object title.
mainDiff[obj] = currentDict
print 'Differences:'
pprint(mainDiff)
答案 1 :(得分:0)
我看到你如何检查引用对象,但引用对象可能有其他键。
所以我会添加一个测试来检查密钥是否相同。我在这里打印,但您可以将其记录到字典或错误列表中。
main_dif = {}
for key, value in dict.iteritems():
dif_k = {}
if value.keys() != ref.keys():
print('Key mismatch for {0}'.format(key))
print('Ref: {0}. Actual: {1}'.format(ref.keys(), value.keys())
for k, v in value.iteritems():
try:
a = ref[k]
except:
a = None
if a != v:
dif_k.update({k:v})
main_dif.update({key:dif_k})
如果您将两个列表转换为集合然后减去它们,那么您可以更好地计算额外或缺少的键。
答案 2 :(得分:0)
您可以尝试这样:
In [17]: refdict
Out[17]:
{'object-1301': {'act': 'false',
'act1': 'false',
'act1x': 'true',
'actAdv': 'false',
'actAe': 'false',
'actArp': 'false'}}
In [18]: mydict
Out[18]:
{'object-218': {'act': 'false',
'act1': 'true',
'act1x': 'false',
'actAdv': 'false',
'actAe': 'true',
'actArt': 'false'}}
for k, v in refdict.iteritems():
for k1, v1 in mydict.iteritems():
if k != k1:
print('Checking for object: {0}'.format(k1))
for k2, v2 in v.iteritems():
if k2 in v1:
if v[k2] == v1[k2]:
continue
else:
print "Differences"
print('For Key {0}, Value {1} are different'.format(k2, v1[k2]))
else:
print "Missing key in current object"
print k2
for k3 in v1:
if k3 not in v:
print "Missing key in reference object"
print k3
#result
Checking for object: object-218
Differences
For Key act1, Value true are different
Differences
For Key actAe, Value true are different
Missing key in current object
actArp
Differences
For Key act1x, Value false are different
Missing key in reference object
actArt