Python - 使用 min() 查找 JSON 对象中的最小值?

Python - Using min() to find min value in JSON object?

所以我有一个 geojson 对象:

"features" : [{"properties": {"rank": 10}},{"properties": {"rank": 2}}]

等等。我想找到最小等级并使用 min 方法。所以我尝试了这样的事情:

features = geojson["features"]
min(features["properties"]["rank"])

然后:

features = geojosn["features"]["properties"]["rank"]
min(features)

并且在两者上都得到了这个:

TypeError: List indices must be integers of slices, not str

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

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

min(feature["properties"]["rank"] for feature in geojson['features'])

或者,如果您想要恢复整个功能,则可以使用密钥:

min(geojson['features'], key=lambda feature: feature["properties"]["rank"])