Python - 使用min()在JSON对象中查找min值?

oce*_*800 1 python json min geojson

所以我有一个geojson对象,它具有:

"features" : [{"properties": {"rank": 10}},{"properties": {"rank": 2}}]
Run Code Online (Sandbox Code Playgroud)

等等.我想找到最低等级并使用min方法.所以我尝试过这样的事情:

features = geojson["features"]
min(features["properties"]["rank"])
Run Code Online (Sandbox Code Playgroud)

然后:

features = geojosn["features"]["properties"]["rank"]
min(features)
Run Code Online (Sandbox Code Playgroud)

并得到了这两个:

TypeError:List索引必须是切片的整数,而不是str

我究竟做错了什么?任何帮助将不胜感激,谢谢!

ACh*_*ion 6

我假设你有一个带属性的功能列表(因为@idjaw指出你的数据结构无效).然后geojson['features']是一个列表,您没有索引列表.您可以使用生成器执行此操作:

min(feature["properties"]["rank"] for feature in geojson['features'])
Run Code Online (Sandbox Code Playgroud)

或者,如果您想要整个功能,那么您可以使用密钥:

min(geojson['features'], key=lambda feature: feature["properties"]["rank"])
Run Code Online (Sandbox Code Playgroud)