字典列表的汇总

时间:2019-03-02 00:34:02

标签: python

我正在尝试对我正在使用的Django应用程序中找到的数据进行排序和聚合。问题是我迷失了遍历列表和存储数据的最佳方法。

这是我所拥有的一个例子:

from score.models import LocData

q = [
        {'ref': '002', 'loc': 'seattle', 'total': '200'}, 
        {'ref': '002', 'loc': 'seattle', 'total': '100'}, 
        {'ref': '123', 'loc': 'dallas', 'total': '100'}, 
        {'ref': '452', 'loc': 'cleveland', 'total': '600'}, 
        {'ref': '123', 'loc': 'dallas', 'total': '200'}, 
        {'ref': '002', 'loc': 'seattle', 'total': '300'}
        ]

我最后想要得到的是下面的列表,它由ref字段聚合,并通过loc维护该字段,但添加了total字段。这是所需的输出。

q = [
        {'ref': '002', 'loc': 'seattle', 'total': '600'}, 
        {'ref': '123', 'loc': 'dallas', 'total': '300'}, 
        {'ref': '452', 'loc': 'cleveland', 'total': '600'}, 
        ]

有人可以向我提示我可以使用哪些工具来执行此操作?预先感谢!

2 个答案:

答案 0 :(得分:2)

您可以先构建一个中间字典,然后根据所需的输出对其进行转换:

from collections import defaultdict

q = [
        {'ref': '002', 'loc': 'seattle', 'total': '200'}, 
        {'ref': '002', 'loc': 'seattle', 'total': '100'}, 
        {'ref': '123', 'loc': 'dallas', 'total': '100'}, 
        {'ref': '452', 'loc': 'cleveland', 'total': '600'}, 
        {'ref': '123', 'loc': 'dallas', 'total': '200'}, 
        {'ref': '002', 'loc': 'seattle', 'total': '300'}
    ]

temp_dict = defaultdict(int)

for entry in q:
    temp_dict[(entry['ref'], entry['loc'])] += int(entry['total'])

result = [{'ref': k[0], 'loc': k[1], 'total': str(v)} for k, v in temp_dict.items()]
print(result)

# [{'ref': '002', 'loc': 'seattle', 'total': '600'},
#  {'ref': '123', 'loc': 'dallas', 'total': '300'},
#  {'ref': '452', 'loc': 'cleveland', 'total': '600'}]

答案 1 :(得分:0)

您可以使用collections.Counter进行汇总:

from collections import Counter
from pprint import pprint

q = [
    {"ref": "002", "loc": "seattle", "total": "200"},
    {"ref": "002", "loc": "seattle", "total": "100"},
    {"ref": "123", "loc": "dallas", "total": "100"},
    {"ref": "452", "loc": "cleveland", "total": "600"},
    {"ref": "123", "loc": "dallas", "total": "200"},
    {"ref": "002", "loc": "seattle", "total": "300"},
]

counts = Counter()
for x in q:
    ref, loc, total = x["ref"], x["loc"], x["total"]
    counts[ref, loc] += int(total)

pprint(
    [
        {"ref": ref, "loc": loc, "total": str(total)}
        for (ref, loc), total in counts.items()
    ]
)
#[{'loc': 'seattle', 'ref': '002', 'total': '600'},
# {'loc': 'dallas', 'ref': '123', 'total': '300'},
# {'loc': 'cleveland', 'ref': '452', 'total': '600'}]