python - 基于Python中的键聚合字典列表上的值

标签 python list dictionary

我试图聚合 2 个不同的列表,其中每个元素都是一个包含 2 个条目、月份和值的字典。

所以第一个列表如下所示:

[{
    'patient_notes': 5,
    'month': datetime.date(2017, 1, 1)
}, {
    'patient_notes': 5,
    'month': datetime.date(2017, 2, 1)
}, {
    'patient_notes': 5,
    'month': datetime.date(2017, 5, 1)
}, {
    'patient_notes': 5,
    'month': datetime.date(2017, 7, 1)
}, {
    'patient_notes': 5,
    'month': datetime.date(2017, 8, 1)
}, {
    'patient_notes': 5,
    'month': datetime.date(2017, 12, 1)
}]

第二个列表是:

[{
    'employee_notes': 4,
    'month': datetime.date(2017, 2, 1)
}, {
    'employee_notes': 4,
    'month': datetime.date(2017, 3, 1)
}, {
    'employee_notes': 4,
    'month': datetime.date(2017, 4, 1)
}, {
    'employee_notes': 4,
    'month': datetime.date(2017, 8, 1)
}, {
    'employee_notes': 4,
    'month': datetime.date(2017, 9, 1)
}, {
    'employee_notes': 4,
    'month': datetime.date(2017, 10, 1)
},  {
    'employee_notes': 4,
    'month': datetime.date(2017, 12, 1)
}]

所以我需要构建一个新列表,其中包含每月两个列表的总和,如下所示:

[{
    'total_messages': 14,
    'month': '2017-01-01'
}, {
    'total_messages': 14,
    'month': '2017-02-01'
}, {
    'total_messages': 14,
    'month': '2017-03-01'
}, {
    'total_messages': 14,
    'month': '2017-04-01'
}, {
    'total_messages': 14,
    'month': '2017-05-01'
}, {
    'total_messages': 14,
    'month': '2017-06-01'
}, {
    'total_messages': 14,
    'month': '2017-07-01'
}, {
    'total_messages': 14,
    'month': '2017-08-01'
}, {
    'total_messages': 14,
    'month': '2017-09-01'
}, {
    'total_messages': 14,
    'month': '2017-10-01'
}, {
    'total_messages': 14,
    'month': '2017-11-01'
}, {
    'total_messages': 14,
    'month': '2017-12-01'
}]

我首先尝试使用 zip,但这仅在前两个列表大小相等时才有效。然后我尝试使用 [itertools.izip_longest] 但如果列表大小相同但月份不同,则会出现问题...我不能简单地聚合这些...我只需要聚合匹配的月份

计数器也很适合这个,但我无法更改原始列表的键名称......有什么想法吗?

最佳答案

您可以使用defaultdict来创建计数器。浏览第一个列表中的每一项,并将 patent_notes 值添加到字典中。然后浏览第二个列表并添加 employee_notes 值。

现在您需要将新的 defaultdict 以您所需的格式编码回列表中。您可以为此使用列表理解。我已按月份对列表进行排序。

from collections import defaultdict

dd = defaultdict(int)

for d in my_list_1:
    dd[d['month']] += d['patient_notes']
for d in my_list_2:
    dd[d['month']] += d['employee_notes']

result = [{'total_messages': dd[k], 'month': k} for k in sorted(dd.keys())]
>>> result
[{'month': datetime.date(2017, 1, 1), 'total_messages': 5},
 {'month': datetime.date(2017, 2, 1), 'total_messages': 9},
 {'month': datetime.date(2017, 3, 1), 'total_messages': 4},
 {'month': datetime.date(2017, 4, 1), 'total_messages': 4},
 {'month': datetime.date(2017, 5, 1), 'total_messages': 5},
 {'month': datetime.date(2017, 7, 1), 'total_messages': 5},
 {'month': datetime.date(2017, 8, 1), 'total_messages': 9},
 {'month': datetime.date(2017, 9, 1), 'total_messages': 4},
 {'month': datetime.date(2017, 10, 1), 'total_messages': 4},
 {'month': datetime.date(2017, 12, 1), 'total_messages': 9}]

关于python - 基于Python中的键聚合字典列表上的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45828229/

相关文章:

python - 如何在 Google Drive API v3 中将 XLSX 转换为表格

python - 对来自两个数据帧的分组数据使用 isin() 函数

Python:在元组列表中查找最接近的匹配项

python - 按值python打印字典键和值

ios - 如何在 swift 中打印字典值数组

python - 我想加载到字典中的逗号分隔文件

python - 如何在字典中使用for循环

python - 找出列表中有多少个二进制序列 '1'

python : Convert list of tuple to dict

Python 嵌套的整数列表未按预期返回