简体   繁体   English

如何从字典列表中的字典中获取键和值

[英]How to get keys and values from a dictionary within a list within a dictionary

I would like to get the values from a list within a dictionary. 我想从字典中的列表中获取值。 I get the following json data structure when I make an Oanda REST-API request. 发出Oanda REST-API请求时,得到以下json数据结构。

When I use the get function I can access the dict and the keys and its values from "trades" and "lastTransactionID", but do not know how to get the list items which are in the "trades" key. 当我使用get函数时,可以从“ trades”和“ lastTransactionID”访问dict和键及其值,但不知道如何获取“ trades”键中的列表项。

I would really appreciate if you could help me to get this running 如果您能帮助我顺利进行,我将不胜感激

Works print rv.get("trades") 作品打印rv.get(“交易”)

Does not work print rv.get("trades").get("financing") 打印rv.get(“ trades”)。get(“ financing”)无效

{
  "trades": [
    {
      "financing": "0.0000", 
      "openTime": "2017-11-08T17:21:49.533679739Z", 
      "price": "1.15901", 
      "unrealizedPL": "-0.0001", 
      "realizedPL": "0.0000", 
      "instrument": "EUR_USD", 
      "state": "OPEN", 
      "initialUnits": "1", 
      "currentUnits": "1", 
      "id": "2046"
    }, 
    {
      "financing": "0.0000", 
      "openTime": "2017-11-08T17:19:27.343697147Z", 
      "price": "1.15905", 
      "unrealizedPL": "-0.0001", 
      "realizedPL": "0.0000", 
      "instrument": "EUR_USD", 
      "state": "OPEN", 
      "initialUnits": "1", 
      "currentUnits": "1", 
      "id": "2044"
    }
  ], 
  "lastTransactionID": "2046"
}

Thanks for your help and kind regards 感谢您的帮助和亲切的问候

Just iterate over the list: 只需遍历列表:

for trade in rv.get("trades"):
    print trade.get("financing")

print rv.get("trades").get("financing") doesn't work because rv.get("trades") returns a list of dictionaries. print rv.get("trades").get("financing")无效,因为rv.get("trades")返回字典列表。 Those dictionaries are the ones with the "financing" key. 这些词典是带有"financing"键的词典。

You just need to iterate over the list: 您只需要遍历列表:

Demo here 在这里演示

 data = {
      "trades": [
        {
          "financing": "0.0000", 
          "openTime": "2017-11-08T17:21:49.533679739Z", 
          "price": "1.15901", 
          "unrealizedPL": "-0.0001", 
          "realizedPL": "0.0000", 
          "instrument": "EUR_USD", 
          "state": "OPEN", 
          "initialUnits": "1", 
          "currentUnits": "1", 
          "id": "2046"
        }, 
        {
          "financing": "0.0000", 
          "openTime": "2017-11-08T17:19:27.343697147Z", 
          "price": "1.15905", 
          "unrealizedPL": "-0.0001", 
          "realizedPL": "0.0000", 
          "instrument": "EUR_USD", 
          "state": "OPEN", 
          "initialUnits": "1", 
          "currentUnits": "1", 
          "id": "2044"
        }
      ], 
      "lastTransactionID": "2046"
    };

    for d in data['trades']:
        print(d['openTime'])

This returns: 返回:

2017-11-08T17:21:49.533679739Z
2017-11-08T17:19:27.343697147Z

rv.get("trades").get("financing") did not work because, trades value is not a dictionary but a list. rv.get("trades").get("financing")无效,因为trades价值不是字典而是列表。 You can check the type of it by doing type(rv.get('trades')) . 您可以通过执行type(rv.get('trades'))来检查其类型。 So the correct way is 所以正确的方法是

rv.get("trades")[0].get("financing")
rv.get("trades")[1].get("financing")

So you need to iterate over the list as 所以你需要遍历列表为

for item in rv.get('trades'):
    print(item.get('financing'))

Another neat alternative would be to use list comprehension, which will allow for a one-line solution: 另一个简洁的选择是使用列表理解,这将允许使用单行解决方案:

[trade.get("financing") for trade in rv.get("trades")]

Note that this is not better than @Bahrom answer, just more pythonic in my opinion. 请注意,这并不比@Bahrom答案好,在我看来只是pythonic。

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

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