gpt4 book ai didi

python - 比较两个字典中的键并使用 for 循环更新值

转载 作者:行者123 更新时间:2023-12-01 08:50:57 24 4
gpt4 key购买 nike

具有点特征的 GeoJson 包含两个属性:CityRating城市作为标识符永远不会改变,但评级将定期更新。新的评级以vales(“dnew”)的形式存储在字典中。我的 for 循环运行得不好。请看下面的代码,其中“#here is the Problem”标记了我无法解决的问题。

import json

dnew = {"Budapest": "fair", "New York": "very good", "Rome": "awesome"}

data = {
"type": "FeatureCollection",
"name": "Cities",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "City": "New York", "Rating": "good" }, "geometry": { "type": "Point", "coordinates": [ -73.991836734693834, 40.736734693877537 ] } },
{ "type": "Feature", "properties": { "City": "Rome", "Rating": "fair" }, "geometry": { "type": "Point", "coordinates": [ 12.494557823129199, 41.903401360544223 ] } },
{ "type": "Feature", "properties": { "City": "Budapest", "Rating": "awesome" }, "geometry": { "type": "Point", "coordinates": [ 19.091836734693832, 47.494557823129256 ] } }
]
}

#at this point, keys of two dictionaies are compared. If they are the same, the value of the old dict is updated/replaced by the value of the new dict
for key in data["features"]:
citykey = (key["properties"]["City"])
ratingvalue = (key["properties"]["Rating"])
#print(citykey + "| " + ratingvalue)

for keynew in dnew:
citynew = (keynew)
ratingnew = dnew[keynew]
#print(citynew + " | " + ratingnew)
print(citykey + "==" + citynew)

if citykey == citynew:
#
#here is the problem
#
data["features"]["properties"]["Rating"] = ratingnew
print(True)
else:
print(False)

错误消息:

TypeError: list indices must be integers or slices, not str

谢谢!

最佳答案

它在“features”之后缺少数字索引,因为它是列表而不是字典。

数据["功能"][0]["属性"]["评级"]

关于python - 比较两个字典中的键并使用 for 循环更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53133516/

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