如何访问位于字典内的字典中的键

问题描述 投票:-1回答:1

例如,请看下面的代码:

{0.1: {'batsman': 'SC Ganguly', 'bowler': 'P Kumar', 'runs': \
      {'batsman': 0, 'total': 1, 'extras': 1}, \
      'extras': {'legbyes': 1}, \
      'non_striker': 'BB McCullum'}}

我想访问字典中的运行和键 - 如何访问它。

这实际上是一个位于词典里面的字典中的值。

python python-2.7 dictionary
1个回答
1
投票

您可以为嵌套字典链接字典键索引器。这是一个例子:

d[0.1]['runs']

输出:

{'batsman': 0, 'extras': 1, 'total': 1}

我已将您的字典定义为d,如下所示:

d = {0.1: {'batsman': 'SC Ganguly', 'bowler': 'P Kumar', 'runs': \
          {'batsman': 0, 'total': 1, 'extras': 1}, \
          'extras': {'legbyes': 1}, \
          'non_striker': 'BB McCullum'}}
© www.soinside.com 2019 - 2024. All rights reserved.