如何从字符串中的公共元素创建字典
How to create dictionary from common elements in a string
我需要将第一个字符相同的字符串中的所有秒字符放在一起。
这是我的代码:
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'])
输出:
GALA BSC
GALA ETH
VIB ETH
FIS BSC
FIS ETH
BAR CHZ
RAD ETH
COTI BSC
COTI BNB
COTI ETH
但我需要像这样在字典中转换它:
GALA = {'BSC', 'ETH'}
VIB = {'ETH'}
FIS = {'BSC', 'ETH'}
BAR = {'CHZ'}
RAD = {'ETH'}
COTI = {'BSC', 'BNB', 'ETH'}
提前致谢!
试试这个:
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)
我需要将第一个字符相同的字符串中的所有秒字符放在一起。
这是我的代码:
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'])
输出:
GALA BSC
GALA ETH
VIB ETH
FIS BSC
FIS ETH
BAR CHZ
RAD ETH
COTI BSC
COTI BNB
COTI ETH
但我需要像这样在字典中转换它:
GALA = {'BSC', 'ETH'}
VIB = {'ETH'}
FIS = {'BSC', 'ETH'}
BAR = {'CHZ'}
RAD = {'ETH'}
COTI = {'BSC', 'BNB', 'ETH'}
提前致谢!
试试这个:
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)