如何聚合默认字典数据

How to aggregage default dict data

我有一个字典列表如下:

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. 列表current_values[]
  2. 阈值的平均值

所以最后我想看:

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

我有一半的工作可以得到 current_values 的列表,但不是全部使用默认的 dict 在那里我可以做类似

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

谁能帮帮我

小菜一碟 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"),
    },
}