简体   繁体   中英

What is the optimal way to parse these strings in Python?

Using Python and given the following unique items, what is the optimal way to find the pair that matches the two names combined? For example, how would you find the tuple that corresponds to the string "BNBBTC" ?

For background, the right index will only ever have ~5 options (of strings length 3 or 4) while the left may have >100.

(AMB, BNB),
(AMB, BTC),
(AMB, ETH),
(ARK, BTC),
(ARK, ETH),
(ARN, BTC),
(ARN, ETH),
(AST, BTC),
(AST, ETH),
(BAT, BNB),
(BAT, BTC),
(BAT, ETH),
(BCC, BNB),
(BCC, BTC),
(BCC, ETH),
(BCC, USDT),
(BCPT, BNB),
(BCPT, BTC),
(BCPT, ETH),
(BNB, BTC),
(BNB, ETH),
(BNB, USDT),
(BNT, BTC),
(BNT, ETH),
(BQX, BTC),
(BQX, ETH),
(BTC, USDT),
(BTG, BTC),
l = [('AMB', 'BNB'), ('AMB', 'BTC')]

d = dict()

for s in l:
    d[''.join(s)] = s


def find_name(key):
    return d[key] if d.get(key) else None


print(find_name('BNBBTC'))
l = [('AMB', 'BNB'), ('AMB', 'BTC'),('BNB', 'BTC')]

def find_name(key):
    for Touple in l:
        Flag = 0
        if (''.join(Touple)) == key: 
            Flag = 1
            break

    return Touple if (Flag == 1) else  False

print(find_name('BNBBTC'))

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