简体   繁体   中英

Pythonic way to reverse search a nested dictionary

I already have a working code, but I would like to improve it (if there is a better way).

Here is an example of a data structure that this code is processing:

{u'error': [],
 u'result': {u'T2JJER-DDEER-A3C45R': {u'cost': u'101.48810',
                                      u'fee': u'0.27402',
                                      u'margin': u'50.74405',
                                      u'misc': u'',
                                      u'oflags': u'',
                                      u'ordertxid': u'O5HCKM-QMBW5-6F4UAB',
                                      u'ordertype': u'limit',
                                      u'pair': u'XXBTZEUR',
                                      u'posstatus': u'open',
                                      u'rollovertm': u'1519410923',
                                      u'terms': u'0.0100% per 4 hours',
                                      u'time': 1519396523.8057,
                                      u'type': u'buy',
                                      u'vol': u'0.10050000',
                                      u'vol_closed': u'0.00000000'},
             u'T4KPEJ-TKDDR-ZOP45C': {u'cost': u'101.38560',
                                      u'fee': u'0.27374',
                                      u'margin': u'50.69280',
                                      u'misc': u'',
                                      u'oflags': u'',
                                      u'ordertxid': u'OU5NOD-L4KLX-AZINT7',
                                      u'ordertype': u'limit',
                                      u'pair': u'XXBTZEUR',
                                      u'posstatus': u'open',
                                      u'rollovertm': u'1519410923',
                                      u'terms': u'0.0100% per 4 hours',
                                      u'time': 1519396523.8057,
                                      u'type': u'buy',
                                      u'vol': u'0.10050000',
                                      u'vol_closed': u'0.00000000'},
             u'TWJP3K-ASTDW-CO7P3Z': {u'cost': u'101.23520',
                                      u'fee': u'0.27333',
                                      u'margin': u'50.61760',
                                      u'misc': u'',
                                      u'oflags': u'',
                                      u'ordertxid': u'OUKPLC-IZTVW-ZMMFZH',
                                      u'ordertype': u'limit',
                                      u'pair': u'XXBTZEUR',
                                      u'posstatus': u'open',
                                      u'rollovertm': u'1519410923',
                                      u'terms': u'0.0100% per 4 hours',
                                      u'time': 1519396523.8057,
                                      u'type': u'buy',
                                      u'vol': u'0.10050000',
                                      u'vol_closed': u'0.00000000'}}}

As you can see the data structure is very regular. I'm writing a code that can find a KEY, which matches the given value of APIresponse['result'][KEY]['ordertxid']

This is the code that I came up with (it contains the data structure, to run it just copy paste into the python console):

def search_dict(DC, MATCH):
    for KEY in DC:
        #print (KEY, DC[KEY]['ordertxid'])
        if DC[KEY]['ordertxid'] == MATCH: return KEY

APIresponse = {u'result': {u'T4KPEJ-TKDDR-ZOP45C': {u'cost': u'101.38560', u'fee': u'0.27374', u'margin': u'50.69280', u'misc': u'', u'oflags': u'', u'ordertxid': u'OU5NOD-L4KLX-AZINT7', u'ordertype': u'limit', u'pair': u'XXBTZEUR', u'posstatus': u'open', u'rollovertm': u'1519410923', u'terms': u'0.0100% per 4 hours', u'time': 1519396523.8057, u'type': u'buy', u'vol': u'0.10050000', u'vol_closed': u'0.00000000'},
                           u'T2JJER-DDEER-A3C45R': {u'cost': u'101.48810', u'fee': u'0.27402', u'margin': u'50.74405', u'misc': u'', u'oflags': u'', u'ordertxid': u'O5HCKM-QMBW5-6F4UAB', u'ordertype': u'limit', u'pair': u'XXBTZEUR', u'posstatus': u'open', u'rollovertm': u'1519410923', u'terms': u'0.0100% per 4 hours', u'time': 1519396523.8057, u'type': u'buy', u'vol': u'0.10050000', u'vol_closed': u'0.00000000'},
                           u'TWJP3K-ASTDW-CO7P3Z': {u'cost': u'101.23520', u'fee': u'0.27333', u'margin': u'50.61760', u'misc': u'', u'oflags': u'', u'ordertxid': u'OUKPLC-IZTVW-ZMMFZH', u'ordertype': u'limit', u'pair': u'XXBTZEUR', u'posstatus': u'open', u'rollovertm': u'1519410923', u'terms': u'0.0100% per 4 hours', u'time': 1519396523.8057, u'type': u'buy', u'vol': u'0.10050000', u'vol_closed': u'0.00000000'}}, u'error': []}

print "Matching key is: %s" % search_dict(APIresponse['result'], "OU5NOD-L4KLX-AZINT7")

Question: Is there a way to write the search_dict subroutine without a for loop? Preferably a one-liner.

By the way, I did search the web and forum for a suitable solution. I found a solution for a simple flat dictionary, but I don't know how to expand it for a nested dictionary.

This one-liner works for a simple flat dictionary:

APIresponse = {u'result': {u'T4KPEJ-TKDDR-ZOP45C': u'OU5NOD-L4KLX-AZINT7',
                           u'T2JJER-DDEER-A3C45R': u'O5HCKM-QMBW5-6F4UAB',
                           u'TWJP3K-ASTDW-CO7P3Z': u'OUKPLC-IZTVW-ZMMFZH'}, u'error': []}
ORDERid = 'O5HCKM-QMBW5-6F4UAB'

TRADEid = [key for key, value in APIresponse['result'].items() if value == ORDERid]
print TRADEid

如果您真的想要单线,则可以执行以下操作:

next((k for k, v in DC.items() if v['ordertxid'] == MATCH), None)

The final code that I implemented is following (in the same style as the code for a simple flat dictionary, show above), it contains one-liner that solves the reverse search of a nested dictionary:

APIresponse = {u'result': {u'T4KPEJ-TKDDR-ZOP45C': {u'cost': u'101.38560', u'fee': u'0.27374', u'margin': u'50.69280', u'misc': u'', u'oflags': u'', u'ordertxid': u'OU5NOD-L4KLX-AZINT7', u'ordertype': u'limit', u'pair': u'XXBTZEUR', u'posstatus': u'open', u'rollovertm': u'1519410923', u'terms': u'0.0100% per 4 hours', u'time': 1519396523.8057, u'type': u'buy', u'vol': u'0.10050000', u'vol_closed': u'0.00000000'},
                           u'T2JJER-DDEER-A3C45R': {u'cost': u'101.48810', u'fee': u'0.27402', u'margin': u'50.74405', u'misc': u'', u'oflags': u'', u'ordertxid': u'O5HCKM-QMBW5-6F4UAB', u'ordertype': u'limit', u'pair': u'XXBTZEUR', u'posstatus': u'open', u'rollovertm': u'1519410923', u'terms': u'0.0100% per 4 hours', u'time': 1519396523.8057, u'type': u'buy', u'vol': u'0.10050000', u'vol_closed': u'0.00000000'},
                           u'TWJP3K-ASTDW-CO7P3Z': {u'cost': u'101.23520', u'fee': u'0.27333', u'margin': u'50.61760', u'misc': u'', u'oflags': u'', u'ordertxid': u'OUKPLC-IZTVW-ZMMFZH', u'ordertype': u'limit', u'pair': u'XXBTZEUR', u'posstatus': u'open', u'rollovertm': u'1519410923', u'terms': u'0.0100% per 4 hours', u'time': 1519396523.8057, u'type': u'buy', u'vol': u'0.10050000', u'vol_closed': u'0.00000000'}}, u'error': []}
ORDERid = 'O5HCKM-QMBW5-6F4UAB'

TRADEid = [key for key, value in APIresponse['result'].items() if value['ordertxid'] == ORDERid]
print TRADEid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM