根据键值汇总列表中的字典 [英] Aggregating dicts within a list based on key value

查看:95
本文介绍了根据键值汇总列表中的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力把头缠在这个头上.我有一个包含多个字典的列表,我想基于两个值进行汇总.示例代码:

I'm struggling to wrap my head around this one. I've got a list with multiple dictionaries that I would like to aggregate based on two values. Example code:

>>> data = [
...     { "regex": ".*ccc-r.*", "age": 44, "count": 224 },
...     { "regex": ".*nft-r.*", "age": 23, "count": 44 },
...     { "regex": ".*ccc-r.*", "age": 44, "count": 20 },
...     { "regex": ".*ccc-r.*", "age": 32, "count": 16 },
...     { "regex": ".*nft-r.*", "age": 23, "count": 46 },
...     { "regex": ".*zxy-r.*", "age": 16, "count": 55 }
...     ]

我正在尝试汇总具有相同年龄和正则表达式的字典,并在所有实例中添加计数键.输出示例为:

I'm trying to aggregate dicts that have the same age and regex and adding the count key across all instances. Example output would be:

>>> data = [
...     { "regex": ".*ccc-r.*", "age": 44, "count": 244 },
...     { "regex": ".*nft-r.*", "age": 23, "count": 90 },
...     { "regex": ".*ccc-r.*", "age": 32, "count": 16 },
...     { "regex": ".*zxy-r.*", "age": 16, "count": 55 }
...     ]

想在没有熊猫或插件模块的情况下执行此操作,如果可能的话,最好使用std库中的解决方案.

Would like to do this without pandas or addon modules, would prefer a solution from the std lib if at all possible.

谢谢!

推荐答案

假设您不想使用任何导入,您可以首先将数据收集到字典中的 aggregated_data 中,其中的密钥将(regex,age)的元组,其值将为 count .形成该词典后,您可以重新构建原来的结构:

Assuming you do not want to use any imports, you can first collect the data in a dictionary aggregated_data in which the key will be a tuple of (regex, age), and the value will be the count. Once you have formed this dictionary, you can form back the original structure you had:

data = [
    { "regex": ".*ccc-r.*", "age": 44, "count": 224 },
    { "regex": ".*nft-r.*", "age": 23, "count": 44 },
    { "regex": ".*ccc-r.*", "age": 44, "count": 20 },
    { "regex": ".*ccc-r.*", "age": 32, "count": 16 },
    { "regex": ".*nft-r.*", "age": 23, "count": 46 },
    { "regex": ".*zxy-r.*", "age": 16, "count": 55 }
]

aggregated_data = {}

for dictionary in data:
    key = (dictionary['regex'], dictionary['age'])
    aggregated_data[key] = aggregated_data.get(key, 0) + dictionary['count']

data = [{'regex': key[0], 'age': key[1], 'count': value} for key, value in aggregated_data.items()]

这篇关于根据键值汇总列表中的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆