python - 将新字典项添加到嵌套字典
问题描述
cities={
'city1':{
'name':'sydney',
'country':'australia',
'desc':'beautiful'
},
'city2':{
'name':'toronto',
'country':'canada',
'desc':'amazing',
}
}
cities['city3']:"{'name':'Tokyo','country':'japan','desc':'lots of earthquakes’}"
for keys,values in cities.items():
print(f"{keys}--->{values}”)
这是我的代码。到目前为止,我是 python 和学习词典的新手。我正在尝试将字典添加到现有字典中,但它不起作用。我没有错误,仍然只获得前两个城市的信息。我认为我的语法一定是错误的。谁能帮我解决这个问题>?
输出:
解决方案
尝试将您的插入代码更改为:
cities['city3'] = {'name':'Tokyo','country':'japan','desc':'lots of earthquakes'}
您可能不想将其添加为字符串,因此请不要使用引号。此外,在描述的末尾有一个错误的引号。
推荐阅读
- php - 将具有多个条目/值的 stdClass 对象转换为 PHP 中的数组
- json - 删除不匹配并添加缺少的 json jq
- firebase - Cloud Firestore 规则中是否允许在 IF 条件上堆叠多个 IF 条件?
- r - 如果列值存在,则添加新行
- c# - 在 Visual Studio Code 中,是否可以在编辑 C# 代码时延迟显示内联错误,也许直到我保存文件?
- python - 多变量曲线拟合
- python - 将ndarray转换为熊猫数据框
- php - 为什么根据 dd() 正确更改变量时返回 null 的值?
- c++ - 将具有条件要求的概念形式化
- python - Django 用户注册认证问题