首页 > 解决方案 > 如何访问嵌套字典中的列表

问题描述

我正在使用一本字典

a = {'subnets': [{'subnet': None}]}

我想访问子网值并设置subnet="something" 我该怎么做?

标签: pythondictionary

解决方案


a={'subnets': [{'subnet': None}]}

b = a['subnets'] # now b = [{'subnet': None}]

c = b[0] # c = {'subnet': None}

d = c['subnet']

# in one line

info = a['subnets'][0]['subnet']

# setting the value to 'something'

a['subnets'][0]['subnet'] = 'something'

推荐阅读