首页 > 解决方案 > 如何聚合默认字典数据

问题描述

我有一个字典列表如下:

result = {
    "resultset": [
        {"name": "DOG", "threshold": Decimal("1.45600000"), "current_value": 124},
        {"name": "DOG", "threshold": Decimal("1.45600000"), "current_value": 14},
        {"name": "DOG", "threshold": Decimal("1.45600000"), "current_value": 1},
        {"name": "CAT", "threshold": Decimal("1.45600000"), "current_value": 24},
        {"name": "CAT", "threshold": Decimal("1.45600000"), "current_value": 4},
    ]
}

现在我想做两件事,基本上是在我得到的地方做一个聚合:

  1. 当前值列表 []
  2. 阈值的平均值

所以最后我想看看:

{
'DOG': {'current_values': [124,14,1], 'threshold': the average of threshold},
'CAT': {'current_values': [24,4] , 'threshold': the average of threshold}
}

我得到了一半的工作,我可以得到 current_values 的列表,但不是整个使用默认的字典,我可以做类似的事情

all_animals  = defaultdict(list)
     for i in result['resultset']:                
           all_animals[i['name']].append(float(i['current_value']))

有人可以帮我吗

标签: pythonpython-3.xdefaultdict

解决方案


小菜一碟defaultdictstatistics

from decimal import Decimal
from collections import defaultdict
import statistics

result = {
    "resultset": [
        {
            "name": "DOG",
            "threshold": Decimal("1.45600000"),
            "current_value": 124,
        },
        {
            "name": "DOG",
            "threshold": Decimal("1.45600000"),
            "current_value": 14,
        },
        {
            "name": "DOG",
            "threshold": Decimal("1.45600000"),
            "current_value": 1,
        },
        {
            "name": "CAT",
            "threshold": Decimal("1.45600000"),
            "current_value": 24,
        },
        {
            "name": "CAT",
            "threshold": Decimal("1.45600000"),
            "current_value": 4,
        },
    ]
}

current_values_by_name = defaultdict(list)
thresholds_by_name = defaultdict(list)
for x in result["resultset"]:
    current_values_by_name[x["name"]].append(x["current_value"])
    thresholds_by_name[x["name"]].append(x["threshold"])

aggregate_result = {
    name: {
        "current_values": current_values_by_name[name],
        "threshold": statistics.mean(thresholds_by_name[name]),
    }
    for name in current_values_by_name
}

print(aggregate_result)

输出

{
    "DOG": {
        "current_values": [124, 14, 1],
        "threshold": Decimal("1.456"),
    },
    "CAT": {
        "current_values": [24, 4],
        "threshold": Decimal("1.456"),
    },
}

推荐阅读