【发布时间】:2020-11-23 02:35:54
【问题描述】:
我正在尝试解析此列表以获取单个 List[dict],其中包含包含所有指标类型数据的字典。
metrics_info = [
{
"metric_type1": [
{
"name": "m10",
"score": "11"
},
{
"name": "m20",
"score": "790"
}
]},
{
"metric_type2": [
{
"name": "m30",
"score": "245"
}
]}
]
现在我创建了一个方法,允许获取特定指标类型的字典列表:
def parse_metrics(metrics_info: List[dict], metric_type: str) -> List[dict]:
for item in metrics_info:
if metric_type in item:
return item[metric_type]
如何获取包含所有指标类型的内部字典的 List[dict]?
结果应该是这样的:
[
{
"name": "m10",
"score": "11"
},
{
"name": "m20",
"score": "790"
},
{
"name": "m30",
"score": "245"
}
]
【问题讨论】:
标签: python json python-3.x dictionary collections