简体   繁体   English

我如何访问字典中的内容,列表中的内容,字典中的内容?

[英]How do I access something within a dictionary, thats within a list, thats within a dictionary?

How would I access the object 'c' in a print statement?我如何在打印语句中访问 object 'c'?

{'symbol': 'BTCUSD', 'totalResults': 1, 'results': [{'o': 41002.26, 'h': 43361, 'l': 40875.51, 'c': 42364.13, 'v': 59454.94294, 't': 1647993599999}]}

Overall code(API KEY REMOVED BUT ISNT THE ISSUE):总体代码(已删除 API 密钥但不是问题):

command = ""
while(command != "q"):
    command = input("Choose [c] for current price, [p] for previous days price, and [q] to quit.")
    if(command == "c"):
        coin = input("Enter currency pair: ")
        crypto_data = (requests.get(f'https://api.finage.co.uk/last/crypto/{coin}?apikey=API_KEY')).json()
        print(f"The current price of {coin} is {crypto_data['price']}")
    
    if(command == "p"):
        coin = input("Enter currency pair: ")
        crypto_data = (requests.get(f'https://api.finage.co.uk/agg/crypto/prev-close/{coin}?apikey=API_KEY')).json()
        *print(f"The previous price of {coin} is {crypto_data['results']['c']}")*
  • being where I get the issue I get back a variety of codes, mostly I cant call a string and it must be a slice or integer在我遇到问题的地方,我得到了各种代码,大部分我不能调用字符串,它必须是一个切片或 integer

I have tried a range statement as well but it also brings back an error我也尝试了一个范围语句,但它也带回了一个错误

TIA! TIA!

The same way you would do it if they were in a dictionary first, then an array, and then a dictionary again:如果它们首先在字典中,然后是数组,然后再次是字典,你将采用相同的方式:

crypto_data = {'symbol': 'BTCUSD', 'totalResults': 1, 'results': [{'o': 41002.26, 'h': 43361, 'l': 40875.51, 'c': 42364.13, 'v': 59454.94294, 't': 1647993599999}]}

print(crypto_data["results"][0]["c"])

This is because:这是因为:

# crypto_data is a dictionary, so you can access items by their key, i.e.
crypto_data["results"]
# which returns an array: [{'o': 41002.26, 'h': 43361, 'l': 40875.51, 'c': 42364.13, 'v': 59454.94294, 't': 1647993599999}]

# so then access items in the array by their index, i.e.
crypto_data["results"][0]
# which returns a dictionary: {'o': 41002.26, 'h': 43361, 'l': 40875.51, 'c': 42364.13, 'v': 59454.94294, 't': 1647993599999}

# so then access items in the dictionary by their key again, i.e.
crypto_data["results"][0]["c"]
# which returns an integer: 42364.13

if如果

myDict = {'symbol': 'BTCUSD', 'totalResults': 1, 'results': [{'o': 41002.26, 'h': 43361, 'l': 40875.51, 'c': 42364.13, 'v': 59454.94294, 't': 1647993599999}]}

then myDict["results"][0]["c"] would return 42364.1那么myDict["results"][0]["c"]将返回 42364.1

It's because despite you know that the dictionary is within a list, you're trying to access the dictionary as if it wasn't within a list.这是因为尽管您知道字典在列表中,但您仍试图访问字典,就好像它不在列表中一样。

This is really important : this API can return more than one dictionary in the results list - see the totalResults key.这非常重要:这个 API 可以在results列表中返回多个字典 - 请参见totalResults键。

The best way to get the results is to do a for ... range loop:获得结果的最佳方法是执行for ... range循环:

for idx in range(crypto_data['totalResults']): print(crypto_data['results'][idx])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM