如何以变量命名Python字典?

如何以变量命名Python字典?,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我试图从coinmarketcap API创建货币信息的dict,并以货币符号命名每个dict。最终目标: # see coinmarketcap data entered into the dict print(BTC['price'] + "\t$" + BTC['marketCap']) # add arbitrary data to the BTC dict that other dicts might not have BTC['founder'] = "Satoshi Nakam

我试图从coinmarketcap API创建货币信息的dict,并以货币符号命名每个dict。最终目标:

# see coinmarketcap data entered into the dict
print(BTC['price']  + "\t$" + BTC['marketCap'])

# add arbitrary data to the BTC dict that other dicts might not have
BTC['founder'] = "Satoshi Nakamoto"

# see data from multiple sources in any given dict
print(BTC['symbol']  + " was founded by " + BTC['founder'])
print(LTC['symbol']  + " " + LTC['isMineable']  + " mineable")

BTC was founded by Satoshi Nakamoto
LTC is mineable
然而,当我得到coinMarketCapTicker['symbol'],它是一个字符串“BTC”时,我很难从该值创建一个名为BTC的dict

import requests
import json

tickerURL = "https://api.coinmarketcap.com/v1/ticker/?limit=100"
request = requests.get(tickerURL)
coinMarketCapTicker = request.json()

while True:
    for x in coinMarketCapTicker

        tickerSymbol = x['symbol']

        # load current symbol's dict with the rest of the data from the API
        tickerSymbol = {"symbol" : x['symbol'], "price" : x['price_usd'], "marketCap" : x['market_cap_usd']}

    break
这使得一个名为“tickerSymbol”的dict被每个符号覆盖,而不是名为BTC、ETH、LTC等的dict

我尝试过这种方法,但不起作用:

x['symbol'] = {"symbol" : x['symbol'], "price" : x['price_usd'], "marketCap" : x['market_cap_usd']}


我有点困了,有什么想法吗?

最终,解决这个问题的一种方法是,为每个符号建立一个字典,里面有所有的细节,一个字典一个级别,里面有每个符号字典

我将dict of dicts命名为'ticker',因此这将在名为'ticker'的主目录dict中创建一个名为'symbol'…的dict。整个符号dict用作ticker[“”],单个元素用作ticker[“”][“”]

在实践中,使用我发布的代码:

ticker[x['symbol']] = {"symbol" : x['symbol'], "price" : x['price_usd'], "marketCap" : x['market_cap_usd']}
在循环中,用符号dicts填充票证,同时填充符号dicts。在休息前,a:

print(ticker['LTC']["price"])

输出“148.227”(Litecoin的当前美元价格),显示从API提要中提取的随机符号中的数据已正确存储并可用。

您可以制作dict的dict。那太好了-除了底部dict需要调用外,我还有同样的问题。我不能为API上的每个符号创建“if symbol=BTC然后BTC{}”,而不为每个可能出现的符号创建if语句,所以我尝试通过编程实现为什么会出现相同的问题?调用外部dict
dict\u of_symbols
或类似的…
dict\u of_symbols={}
然后调用
dict\u of_symbols['ETH']={……}
。这是一个字典的字典。
dict\u of_symbols[x['symbol']]={…}
print(ticker['LTC']["price"])