gpt4 book ai didi

python - 如何将新对象添加到嵌套字典中?

转载 作者:太空宇宙 更新时间:2023-11-04 10:24:23 24 4
gpt4 key购买 nike

给定以下对象,如何在“加州”中添加一个新城市“洛杉矶”?给定国家、州、城市和城市的属性,我该如何更新对象? (我对 python 很陌生)

myplaces = {
"unitedstates": {
"california": {
"sanfrancisco": {
"description": "Tech heaven",
"population": 1234,
"keyplaces": ["someplace1",
"someplace2"]
}
}
},
"india": {
"karnataka": {
"bangalore": {
"description": "IT hub of India",
"population": 12345,
"keyplaces": ["jpnagar",
"brigade"]
},
"mysore": {
"description": "hostoric place",
"population": 12345,
"keyplaces": ["mysorepalace"]
}
}
}
}

我尝试添加如下元素(就像在 PHP 中的做法一样):

myplaces['unitedstates']['california']['losangeles']['description'] = 'Entertainment'

编辑:这不是重复的。我正在寻找一种添加项目的通用方法。

最佳答案

你快搞定了 -

myplaces['unitedstates']['california']['losangeles'] = {'description':'Entertainment'}

关于python - 如何将新对象添加到嵌套字典中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30257918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com