簡體   English   中英

如何在嵌套字典中返回鍵/值對,其值是包含所述字典的列表

[英]How to return key/value pairs in nested dict whose value is a list that contains said dict

如您所見,問題本身很復雜,但這個問題也是如此。 我正在嘗試在此 API output 中獲取名稱:名稱、符號:符號和價格:價格的鍵/值對。 我已經嘗試了幾件事,但似乎無法弄清楚。 任何幫助表示贊賞。

這是字典:

data = {
    'coins':
            [
                {
                    'id': 'bitcoin',
                    'icon': 'https://static.coinstats.app/coins/Bitcoin6l39t.png',
                    'name': 'Bitcoin',
                    'symbol': 'BTC',
                    'rank': 1,
                    'price': 48918.27974434776,
                    'priceBtc': 1,
                    'volume': 44805486777.77573,
                    'marketCap': 924655133704.012,
                    'availableSupply': 18902037,
                    'totalSupply': 21000000,
                    'priceChange1h': 0.05,
                    'priceChange1d': 1.22,
                    'priceChange1w': -3.02,
                    'websiteUrl': 'http://www.bitcoin.org',
                    'twitterUrl': 'https://twitter.com/bitcoin',
                    'exp': [
                            'https://blockchair.com/bitcoin/',
                            'https://btc.com/', 'https://btc.tokenview.com/'
                            ]
                },

                {
                    'id': 'ethereum',
                    'icon': 'https://static.coinstats.app/coins/EthereumOCjgD.png',
                    'name': 'Ethereum',
                    'symbol': 'ETH',
                    'rank': 2,
                    'price': 4037.7735178501143,
                    'priceBtc': 0.08254618952631135,
                    'volume': 37993691263.19707,
                    'marketCap': 479507778421.57574,
                    'availableSupply': 118755491.4365,
                    'totalSupply': 0,
                    'priceChange1h': 0.3,
                    'priceChange1d': 4.53,
                    'priceChange1w': -8.85,
                    'websiteUrl': 'https://www.ethereum.org/',
                    'twitterUrl': 'https://twitter.com/ethereum',
                    'contractAddress': '0x2170ed0880ac9a755fd29b2688956bd959f933f8',
                    'decimals': 18,
                    'exp': [
                        'https://etherscan.io/',
                        'https://ethplorer.io/',
                        'https://blockchair.com/ethereum',
                        'https://eth.tokenview.com/'
                            ]
                }
            ]
        }

這是我失敗的嘗試之一:

print(data['coins'][1])
new_dict = data['coins'][1]
for i in new_dict:
    a = i.split(',')
    print(a)

我不確定預期的 output 是什么,因為您沒有澄清,盡管您可以訪問每只股票的每個鍵值:

for stock in data["coins"]:
    print(stock["name"] + " - " + stock["symbol"] + " - " + str(stock["price"]))

Output:

Bitcoin - BTC - 48918.27974434776
Ethereum - ETH - 4037.7735178501143

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM