Trouble with Conditional Statements and Matching Lists (Python)

Viewed 34

I have two lists like:

  1. "mexc_names" mexc_names =['ogn_usdt','qnt_usdt',btc_tryb', sbr_usdt',kton_eth,'kton_eth','kton_eth']
  2. "kraken_names" kraken_names = ['ognusd','qnteur', aaveeth, 'sbrusd', 'aaveeth', 'adaeth',' algoeth']

I'm able to return both names in each list if the first half of a string in mexc_names (before the "_") is contained in any string in kraken_names. Done by doing this:

    for y in kraken_names:
        if x.partition("_")[0] in y:
            print(x,y)

Where I get something like:

ogn_usdt ognusd
qnt_usdt qnteur
sbr_usdt sbrusd

However, I want to return matching strings if the first half of a string in mexc_names (before the "_") is in a string in kraken_names AND if the second half of the string in mexc_names is in a string in kraken names.

So I do this:

    for y in kraken_names:
        if (x.partition("_")[0] and x.partition("_")[2]) in y:
            print(x,y)

And get the result:

kton_eth adaeth
kton_eth algoeth
kton_eth anteth
kton_eth atometh
kton_eth baleth
kton_eth bateth
kton_eth bcheth

Sadly, this seems just to get strings that contain the second half of the string in mexc_names but not the first.

I'm looking to get something like:

ogn_usd ognusd
qnt_usdt qntusd
sbr_usd sbrusd
1 Answers

Instead of doing this:

for y in kraken_names:
    if (x.partition("_")[0] and x.partition("_")[2]) in y:
        print(x,y)

Do:

for y in kraken_names:
    if x.partition("_")[0] in y and x.partition("_")[2] in y:
        print(x,y)

Or:

for y in kraken_names:
    split_x = x.split("_")
    if split_x[0] in y and split_x[1] in y:
        print(x,y)

Or:

for y in kraken_names:
    if all(z in y for z in x.split('_')):
        print(x,y)
Related