循环遍历嵌套列表并检索属性(python)

循环遍历嵌套列表并检索属性(python),python,Python,我正在尝试循环通过一个json对象来检索名为matched bets的列表中的数据。当我尝试此操作时,我得到一个KeyError:“matched bets” 如何从嵌套列表中检索属性 data =[ { 'TIMESTAMP': '2019-01-20 04:56:54.609618', 'id': 1019913470680020, 'event-id': 1018510669860015, 'event-name': 'Tomas Berdych vs

我正在尝试循环通过一个json对象来检索名为matched bets的列表中的数据。当我尝试此操作时,我得到一个KeyError:“matched bets”

如何从嵌套列表中检索属性

data =[
  {
    'TIMESTAMP': '2019-01-20 04:56:54.609618',
    'id': 1019913470680020,
    'event-id': 1018510669860015,
    'event-name': 'Tomas Berdych vs Rafael Nadal',
    'market-id': 1018510670260016,
    'market-name': 'Moneyline',
    'market-type': 'money_line',
    'runner-id': 1018510670340016,
    'runner-name': 'Rafael Nadal',
    'sport-id': 9,
    'temp-id': '2.1547948212610',
    'exchange-type': 'back-lay',
    'side': 'back',
    'odds': 3.0,
    'odds-type': 'DECIMAL',
    'decimal-odds': 3.0,
    'stake': 3,
    'remaining': 3,
    'potential-profit': 6.0,
    'remaining-potential-profit': 6.0,
    'commission-type': 'NET_WIN',
    'originator-commission-rate': 0.02,
    'acceptor-commission-rate': 0.02,
    'commission-reserve': 0,
    'currency': 'EUR',
    'created-at': '2019-01-20T01:36:58.498Z',
    'status': 'flushed',
    'in-play': False,
    'Latency': 0.074342
  },
  {
    'TIMESTAMP': '2019-01-20 04:56:54.609618',
    'id': 1019938516780022,
    'event-id': 1018426645920015,
    'event-name': 'Frances Tiafoe vs Grigor Dimitrov',
    'market-id': 1018426646320016,
    'market-name': 'Moneyline',
    'market-type': 'money_line',
    'runner-id': 1018426646390015,
    'runner-name': 'Frances Tiafoe',
    'sport-id': 9,
    'temp-id': '3.1547950690507',
    'exchange-type': 'back-lay',
    'side': 'back',
    'odds': 3.5,
    'odds-type': 'DECIMAL',
    'decimal-odds': 3.5,
    'stake': 200,
    'remaining': 1e-05,
    'potential-profit': 500.0,
    'remaining-potential-profit': 3e-05,
    'commission-type': 'NET_WIN',
    'originator-commission-rate': 0.02,
    'acceptor-commission-rate': 0.02,
    'commission-reserve': 0,
    'currency': 'EUR',
    'created-at': '2019-01-20T02:18:43.108Z',
    'status': 'matched',
    'in-play': True,
    'matched-bets': [
      {
        'id': 1019941163310322,
        'offer-id': 1019938516780022,
        'odds': 3.5,
        'odds-type': 'DECIMAL',
        'decimal-odds': 3.5,
        'stake': 102.84399,
        'potential-profit': 257.10998,
        'commission': 2.0568798,
        'currency': 'EUR',
        'status': 'open',
        'created-at': '2019-01-20T02:23:07.761Z'
      },
      {
        'id': 1019941305850222,
        'offer-id': 1019938516780022,
        'odds': 3.5,
        'odds-type': 'DECIMAL',
        'decimal-odds': 3.5,
        'stake': 97.156,
        'potential-profit': 242.89,
        'commission': 1.94312,
        'currency': 'EUR',
        'status': 'open',
        'created-at': '2019-01-20T02:23:22.015Z'
      }
    ],
    'Latency': 0.080931
  },
  {
    'TIMESTAMP': '2019-01-20 04:56:54.609618',
    'id': 1020003691980022,
    'event-id': 1018426645920015,
    'event-name': 'Frances Tiafoe vs Grigor Dimitrov',
    'market-id': 1018426646320016,
    'market-name': 'Moneyline',
    'market-type': 'money_line',
    'runner-id': 1018426646390015,
    'runner-name': 'Frances Tiafoe',
    'sport-id': 9,
    'temp-id': '1.1547951656615',
    'exchange-type': 'back-lay',
    'side': 'lay',
    'odds': 1.25641,
    'odds-type': 'DECIMAL',
    'decimal-odds': 1.25642,
    'stake': 200,
    'remaining': 0.0,
    'potential-liability': 51.28205,
    'remaining-potential-liability': 0.0,
    'commission-type': 'NET_WIN',
    'originator-commission-rate': 0.02,
    'acceptor-commission-rate': 0.02,
    'commission-reserve': 0,
    'currency': 'EUR',
    'created-at': '2019-01-20T04:07:20.628Z',
    'status': 'matched',
    'in-play': True,
    'matched-bets': [
      {
        'id': 1020008680510122,
        'offer-id': 1020003691980022,
        'odds': 1.25641,
        'odds-type': 'DECIMAL',
        'decimal-odds': 1.25642,
        'stake': 200.0,
        'potential-liability': 51.28205,
        'commission': 1.025641,
        'currency': 'EUR',
        'status': 'open',
        'created-at': '2019-01-20T04:15:39.481Z'
      }
    ],
    'Latency': 0.08113
  },
  {
    'TIMESTAMP': '2019-01-20 04:56:54.609618',
    'id': 1020020358220022,
    'event-id': 1018559986530015,
    'event-name': 'Danielle Rose Collins vs Angelique Kerber',
    'market-id': 1018559986940016,
    'market-name': 'Moneyline',
    'market-type': 'money_line',
    'runner-id': 1018559987020016,
    'runner-name': 'Angelique Kerber',
    'sport-id': 9,
    'temp-id': '5.1547958894686',
    'exchange-type': 'back-lay',
    'side': 'back',
    'odds': 2.36,
    'odds-type': 'DECIMAL',
    'decimal-odds': 2.36,
    'stake': 200,
    'remaining': 1e-05,
    'potential-profit': 272.0,
    'remaining-potential-profit': 1e-05,
    'commission-type': 'NET_WIN',
    'originator-commission-rate': 0.02,
    'acceptor-commission-rate': 0.02,
    'commission-reserve': 0,
    'currency': 'EUR',
    'created-at': '2019-01-20T04:35:07.252Z',
    'status': 'matched',
    'in-play': True,
    'matched-bets': [
      {
        'id': 1020020481880222,
        'offer-id': 1020020358220022,
        'odds': 2.36,
        'odds-type': 'DECIMAL',
        'decimal-odds': 2.36,
        'stake': 199.99999,
        'potential-profit': 271.99999,
        'commission': 3.9999998,
        'currency': 'EUR',
        'status': 'open',
        'created-at': '2019-01-20T04:35:19.618Z'
      }
    ],
    'Latency': 0.08125
  }
]

for d in data:
   id = d['matched-bets'][0]['id']
   print(id)
我尝试了你的json对象。我在游戏中变为:假,在游戏中变为:假,它工作得很好

尝试将false更改为false

更新:

如果使用更新的数据格式,则使用:

for d in data:
     print(d['id'])
你可以这样写

for _d in data:
    if hasattr(_d, 'matched-bets'):
        print(_d['matched-bets']['id'])

在数据变量中设置的内容与粘贴在此处的内容之间很可能存在差异。您似乎已经通过假布尔值直接复制了json,因此请确认代码中实际可用的字典看起来是正确的,并且已正确读取..@WiggyA。我刚刚用完整的json响应更新了这个问题。在我看来,这也是正确的。问题是我正在从第三方api请求此数据,因此如果数据的大小写为bool,这是否意味着json无效?我是否可以在响应后将false更改为false?我已发送了一个新请求,并且新数据的大小写为false而不是小写。我已经更新了这个问题。我还在收到钥匙error@ShaifkurRahmanv我一直收到一个类型错误,更新后的答案id=d['matched-bets'][0]['id']TypeError:字符串索引必须是整数json格式是什么。您可以在问题中更改json格式
for _d in data:
    if hasattr(_d, 'matched-bets'):
        print(_d['matched-bets']['id'])