【问题标题】:How to access dictionaries of dictionaries in python如何在python中访问字典的字典
【发布时间】:2021-09-20 12:00:53
【问题描述】:
a=[{'1st innings': {'team': 'Islamabad United',
   'deliveries': [{0.1: {'non_striker': 'Sharjeel Khan',
      'bowler': 'Anwar Ali',
      'runs': {'extras': 0, 'total': 0, 'batsman': 0},
      'batsman': 'SR Watson'}},
    {0.2: {'non_striker': 'Sharjeel Khan',
      'bowler': 'Anwar Ali',
      'runs': {'extras': 0, 'total': 0, 'batsman': 0},
      'batsman': 'SR Watson'}},
    {0.3: {'non_striker': 'Sharjeel Khan',
      'bowler': 'Anwar Ali',
      'runs': {'extras': 0, 'total': 0, 'batsman': 0},
      'batsman': 'SR Watson'}},
    {0.4: {'non_striker': 'Sharjeel Khan',
      'bowler': 'Anwar Ali',
      'runs': {'extras': 0, 'total': 0, 'batsman': 0},
      'batsman': 'SR Watson'}},
    {0.5: {'non_striker': 'Sharjeel Khan',
      'bowler': 'Anwar Ali',
      'runs': {'extras': 0, 'total': 0, 'batsman': 0},
      'batsman': 'SR Watson'}},
    {0.6: {'non_striker': 'Sharjeel Khan',
      'bowler': 'Anwar Ali',
      'runs': {'extras': 0, 'total': 0, 'batsman': 0},
      'batsman': 'SR Watson'}}}]

我如何在 python 中访问这本词典的不同索引,即'1st innings'、'batsman'、'total'等

当我尝试使用 a["1st innings"] 访问第一局时,我不断收到错误 "TypeError: list indices must be integers or slices, not str" >

【问题讨论】:

  • 你有一个列表,里面有一个字典。寻找 [.
  • 尝试 [0]["1st innings"]
  • 'a' 是一个列表,而不是字典,因此使用 "1st innings"(这是一个键值)不起作用,如错误消息所述。

标签: python list dictionary indices


【解决方案1】:

您有一个 - listdicts。所以你需要访问like

print(a[0]['1st innings'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2023-03-22
    • 2012-05-20
    • 2020-01-16
    相关资源
    最近更新 更多