从深度嵌套的字典中选择条目
Selecting entries from deeply nested dictionary
我有一个嵌套很深的字典 (geojson),其中包含具有特征的 shapefile。
这些功能之一是 'month_num',我想通过它 select 数据。
问题是这个字典嵌套很深。
我想到的最接近的是:
list(filter(lambda country: ['features'][country]['properties']['month_num'] == 2, geojson_countries))
但这给了我以下错误:
TypeError: list indices must be integers or slices, not str
geojson 文件如下所示:
我希望能够 select 所有具有 'month_num' == 2 的条目。
有人可以帮忙吗?
你可能会相处
dct = your_dict.copy()
dct["features"] = [item for item in dct["features"]
if item["properties"]["month_num"] == 2]
我有一个嵌套很深的字典 (geojson),其中包含具有特征的 shapefile。 这些功能之一是 'month_num',我想通过它 select 数据。 问题是这个字典嵌套很深。 我想到的最接近的是:
list(filter(lambda country: ['features'][country]['properties']['month_num'] == 2, geojson_countries))
但这给了我以下错误:
TypeError: list indices must be integers or slices, not str
geojson 文件如下所示:
我希望能够 select 所有具有 'month_num' == 2 的条目。
有人可以帮忙吗?
你可能会相处
dct = your_dict.copy()
dct["features"] = [item for item in dct["features"]
if item["properties"]["month_num"] == 2]