简体   繁体   English

反向搜索嵌套字典的Python方法

[英]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'] 我正在编写一个代码,该代码可以找到一个与APIresponse['result'][KEY]['ordertxid']的给定值匹配的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): 这是我想出的代码(它包含数据结构,要运行它,只需将粘贴复制到python控制台中即可):

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? 问题:有没有没有for循环的方法来编写search_dict子例程? 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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