python - 如何访问嵌套字典中的列表
问题描述
我正在使用一本字典
a = {'subnets': [{'subnet': None}]}
我想访问子网值并设置subnet="something"
我该怎么做?
解决方案
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'
推荐阅读
- java - Android - 如何正确编写 JSONObject 字符串
- java - java 是否会以一致的方式对 ConcurrentHashMap 的值求和?
- sql - SQL 查询未找到 SUM 和 COUNT 中的所有行
- python - 如何在 python 中读取用 pkzip 编写的 zip 文件?
- javascript - 使用 useState 挂钩时仅更新部分状态并保持其他状态不变
- c# - 转义和取消转义无效字符
- android - Android Studio 导入的 SVG 文件(生成 XML)使应用程序崩溃(原因:android.content.res.Resources$NotFoundException)
- postgresql - postgres postgis 2.5 数据库中不存在 pgis_abs
- sql - 无法从 SQL Server 2012/2014/2016 中的 SQL 查询发送电子邮件
- amazon-web-services - 在 AWS 中构建无服务器侦听器的最佳实践