如何访问字典里的关键字

2024-10-05 10:00:25 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,请参见以下代码:

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

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

这实际上是访问位于dictionary内部的dictionary中的值


Tags: 代码extrasdictionaryrunstotalscnonbb
1条回答
网友
1楼 · 发布于 2024-10-05 10:00:25

可以为嵌套词典链接词典键索引器。举个例子:

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'}}

相关问题 更多 >

    热门问题