比较其他嵌套字典

时间:2017-12-06 11:10:07

标签: python python-3.x python-2.7 dictionary nested-loops

我试图递归地比较下面两个python词典:

expectededr = {'uid': 'e579b8cb-7d9f-4c0b-97de-a03bb52a1ec3', 'attempted': {'smpp': {'registeredDelivery': 0}, 'status': 'success', 'OATON': 1, 'OANPI': 1, 'DATON': 1, 'DANPI': 1, 'OA': '12149921220', 'DA': '1514525404'}, 'customerID': 'customer01', 'productID': 'product'}

edr = {'Category': 'NO', 'Type': 'mt', 'uid': 'e579b8cb-7d9f-4c0b-97de-a03bb52a1ec3', 'protocolID': 'smpp', 'direction': 'attempted', 'attempted': {'status': 'success', 'OANPI': 1, 'DATON': 1, 't2': 1512549691602, 'DANPI': 1, 'OA': '12149921220', 'DA': '1514525404', 'smpp': {'fragmented': False, 'sequenceID': 1, 'registeredDelivery': 0, 'messageID': '4e7b48ad-b39e-4e91-a7bb-2de463e4a6ee', 'srcPort': 39417, 'messageType': 4, 'Status': 0, 'ESMClass': 0, 'dstPort': 0, 'size': 0}, 'OATON': 1, 'PID': 0, 't1': 1512549691602}, 'customerID': 'customer01', 'productID': 'product'}  

我试图比较一种方法,即在第二个字段中找到并比较第一个字典的键值和值,然后匹配print PASS其他print FAIL

for key in expectededr:
   if expectededr[key] == edr[key]:
       print("PASS")
   else:
       print("FAIL")

输出:

FAIL
PASS
PASS
PASS

以上代码无法比较所有键和值,因为这些是嵌套词典 正如你在下面看到的,如果我打印上面的键和值,我看到它不在子词典中并且错过了它们的键:

for key in expectededr:
    if expectededr[key] == edr[key]:
        print(expectededr[key])
        print(edr[key])

输出:

customer01
customer01
e579b8cb-7d9f-4c0b-97de-a03bb52a1ec3
e579b8cb-7d9f-4c0b-97de-a03bb52a1ec3
product
product

有人可以帮助更新此代码,以便我可以在这些嵌套字典中进行比较吗?

2 个答案:

答案 0 :(得分:1)

一种方法是压缩字典,然后比较密钥是否匹配。

所以让我们首先初步了解你的决定:

In [23]: expectededr = {'uid': 'e579b8cb-7d9f-4c0b-97de-a03bb52a1ec3', 'attempted': {'smpp': {'registeredDelivery': 0}, 'status': 'success', 'OATON': 1, 'OANP
    ...: I': 1, 'DATON': 1, 'DANPI': 1, 'OA': '12149921220', 'DA': '1514525404'}, 'customerID': 'customer01', 'productID': 'product'}
    ...: 
    ...: edr = {'Category': 'NO', 'Type': 'mt', 'uid': 'e579b8cb-7d9f-4c0b-97de-a03bb52a1ec3', 'protocolID': 'smpp', 'direction': 'attempted', 'attempted': {'
    ...: status': 'success', 'OANPI': 1, 'DATON': 1, 't2': 1512549691602, 'DANPI': 1, 'OA': '12149921220', 'DA': '1514525404', 'smpp': {'fragmented': False, '
    ...: sequenceID': 1, 'registeredDelivery': 0, 'messageID': '4e7b48ad-b39e-4e91-a7bb-2de463e4a6ee', 'srcPort': 39417, 'messageType': 4, 'Status': 0, 'ESMCl
    ...: ass': 0, 'dstPort': 0, 'size': 0}, 'OATON': 1, 'PID': 0, 't1': 1512549691602}, 'customerID': 'customer01', 'productID': 'product'}  
    ...: 

为了展示您的词典,我们可以使用the approach suggested in Flatten nested Python dictionaries, compressing keys

In [24]: import collections
    ...: 
    ...: def flatten(d, parent_key='', sep='_'):
    ...:     items = []
    ...:     for k, v in d.items():
    ...:         new_key = parent_key + sep + k if parent_key else k
    ...:         if isinstance(v, collections.MutableMapping):
    ...:             items.extend(flatten(v, new_key, sep=sep).items())
    ...:         else:
    ...:             items.append((new_key, v))
    ...:     return dict(items)
    ...: 

并生成平坦的词汇

In [25]: flat_expectededr = flatten(expectededr)

In [26]: flat_edr = flatten(edr)

现在进行简单的比较:

In [27]: for key in flat_expectededr:
    ...:     if flat_edr.get(key) == flat_expectededr[key]:
    ...:         print "PASS"
    ...:     else:
    ...:         print "FAIL"
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS

答案 1 :(得分:0)

简单方法:

<uses-permission-sdk-23 android:name="android.permission.SEND_SMS"/>