Search code examples
pythonpython-3.xtradingalgorithmic-tradingcryptocurrency

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

Solution

  • 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'))