比较两个字典中的键并使用 for 循环更新值
compare keys in two dictionaries and update values with for loops
具有点特征的 GeoJson 包含两个属性:City 和 Rating。
City 作为标识符永远不会改变,但 Rating 会定期更新。
新的 Rating 作为值 ("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" 之后缺少数字索引,因为它是列表而不是字典。
data["features"][0]["properties"]["Rating"]
您为 data['features']
列表中的每个元素遍历 dnew
字典中的所有键,从而错过了字典的好处。
E.Coms 注意到您遇到的问题,但只检查列表中的第一项,(data["features"][0]
)
也许下面的内容可以解决您的问题。
for key in data["features"]:
citykey = (key["properties"]["City"])
ratingvalue = (key["properties"]["Rating"])
#print(citykey + "| " + ratingvalue)
if citykey in dnew:
key["properties"]["Rating"] = dnew[citykey]
具有点特征的 GeoJson 包含两个属性:City 和 Rating。 City 作为标识符永远不会改变,但 Rating 会定期更新。 新的 Rating 作为值 ("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" 之后缺少数字索引,因为它是列表而不是字典。
data["features"][0]["properties"]["Rating"]
您为 data['features']
列表中的每个元素遍历 dnew
字典中的所有键,从而错过了字典的好处。
E.Coms 注意到您遇到的问题,但只检查列表中的第一项,(data["features"][0]
)
也许下面的内容可以解决您的问题。
for key in data["features"]:
citykey = (key["properties"]["City"])
ratingvalue = (key["properties"]["Rating"])
#print(citykey + "| " + ratingvalue)
if citykey in dnew:
key["properties"]["Rating"] = dnew[citykey]