Home > database >  How to create dictionary from common elements in a string
How to create dictionary from common elements in a string

Time:04-26

I need to put all the seconds character from a string that have the same first character in common.

This is my code:

Coin_Info = Binance_Client.get_all_coins_info()
for n in Coin_Info:
    Coin, Net = n['coin'], n['networkList']
    for e in Net:
        if e['depositEnable'] and e['withdrawEnable'] is True:
            print(Coin, e['network'])

output:

GALA BSC
GALA ETH
VIB ETH
FIS BSC
FIS ETH
BAR CHZ
RAD ETH
COTI BSC
COTI BNB
COTI ETH

but i need to transform it in a dictionary like this:

GALA = {'BSC', 'ETH'}
VIB = {'ETH'}
FIS = {'BSC', 'ETH'}
BAR = {'CHZ'}
RAD = {'ETH'}
COTI = {'BSC', 'BNB', 'ETH'}

thank you in advance!

CodePudding user response:

Try this:

my_dict = {}
Coin_Info = Binance_Client.get_all_coins_info()
for n in Coin_Info:
    Coin, Net = n['coin'], n['networkList']
    for e in Net:
        if e['depositEnable'] and e['withdrawEnable']:
            if not my_dict.hasKey(Coin):
                my_dict[Coin] = []
            my_dict[Coin].append(e['network'])
print(my_dict)
  • Related