Python: 压平多个嵌套的字典并追加

Python: Flatten multiple nested dict and append

我 大家好, 我正在寻求帮助,试图压平多个嵌套的字典并将它们附加到新列表中。 我有多个字典,从这样的 geojson-File 加载:

data = json.load(open("xy.geojson"))

它们的结构都是这样的:

{'type': 'Feature', 'properties': {'tags': {'highway': 'cycleway', 'lit': 'yes', 'source': 'survey 08.2018 and Esri', 'surface': 'paving_stones', 'traffic_sign': 'DE:237 Radweg'}}, 'geometry': {'type': 'LineString', 'coordinates': [[6.7976974, 51.1935231], [6.7977131, 51.1935542], [6.7977735, 51.1935719], [6.7978679, 51.193578], [6.798005, 51.1936044], [6.7982118, 51.1936419], [6.7983474, 51.1936511], [6.7984899, 51.1936365], [6.7985761, 51.193623], [6.7986739, 51.1936186], [6.7987574, 51.1936188], [6.7988269, 51.1936342], [6.7988893, 51.1936529], [6.7989378, 51.1936778], [6.7990085, 51.1937739]]}}

现在我想展平字典的 'tags' 部分,所以我得到:

{'type': 'Feature', 'properties': {'highway': 'cycleway', 'lit': 'yes', 'source': 'survey 08.2018 and Esri', 'surface': 'paving_stones', 'traffic_sign': 'DE:237 Radweg'}, 'geometry': {'type': 'LineString', 'coordinates': [[6.7976974, 51.1935231], [6.7977131, 51.1935542], [6.7977735, 51.1935719], [6.7978679, 51.193578], [6.798005, 51.1936044], [6.7982118, 51.1936419], [6.7983474, 51.1936511], [6.7984899, 51.1936365], [6.7985761, 51.193623], [6.7986739, 51.1936186], [6.7987574, 51.1936188], [6.7988269, 51.1936342], [6.7988893, 51.1936529], [6.7989378, 51.1936778], [6.7990085, 51.1937739]]}}

到目前为止我所做的是设置一个新列表并启动一个 for 循环:

filtered = []
for geo in data['features']:

但是我如何在循环中展平 geo['properties']['tags'] 并将每个字典的结果附加到过滤? 非常感谢大家,感谢您的帮助! 克莱门斯

可能有更好的方法,但这似乎有效:

filtered = []
for geo in data["features"]:
    updated = dict(**geo)
    updated["properties"] = geo["properties"]["tags"]
    filtered.append(updated)

print(filtered)