Ad

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),
Ad

Answer

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'))
Ad
source: stackoverflow.com
Ad