将 defaultdict(list) 的总和映射到一个列表

Mapping sums of defaultdict(list) to one list

我收集了大量数据,其格式有点像 defaultdict(list)d.items()。见下文:

products = [(('blue'), ([2, 4, 2, 4, 2, 4, 2, 4, 2, 4], [2, 4, 2, 4, 2, 4, 2, 4, 2, 4], [2, 4, 2, 4, 2, 4, 2, 4, 2, 4])),
        (('yellow'), ([1, 3, 1, 3, 1, 3, 1, 3, 1, 3], [1, 3, 1, 3, 1, 3, 1, 3, 1, 3], [1, 3, 1, 3, 1, 3, 1, 3, 1, 3])),
        (('red'), ([1, 1, 1, 1, 1, 2, 5, 4, 6, 4], [2, 5, 3, 4, 8, 1, 1, 1, 1, 1], [8, 6, 3, 9, 2, 1, 1, 1, 1, 1]))]

我想将嵌套列表中每个数据值的总和映射到相同位置或索引中的相应对应值,以产生如下最终总和:

['blue', 6, 12, 6, 12, 6, 12, 6, 12, '6.000000', 12]
['yellow', 3, 9, 3, 9, 3, 9, 3, 9, '3.000000', 9]
['red', 11, 12, 7, 14, 11, 4, 7, 6, '8.000000', 6]

使用循环,可以很容易地完成,如这个函数所示:

def summation(products):

    sums = []

    for item in products:
            sums.append([(item[0]),
                     sum(int(x[0]) for x in item[1]),
                     sum(int(x[1]) for x in item[1]),
                     sum(int(x[2]) for x in item[1]),
                     sum(int(x[3]) for x in item[1]),
                     sum(int(x[4]) for x in item[1]),
                     sum(int(x[5]) for x in item[1]),
                     sum(int(x[6]) for x in item[1]),
                     sum(int(x[7]) for x in item[1]),
                     "{:.6f}".format(sum(float(x[8]) for x in item[1])),
                     sum(int(x[9]) for x in item[1])])

    for s in sums:
        print(s)

当产品规模为百万级时,就会出现问题,即非常耗时。所以我想到了实现将每个值映射到同一键的嵌套列表中的相应值。这是我试过的:

def mappingSum(products):
    sums = []

    for item in products:
        sums.append([item[0], map((sum(x), sum(y), sum(z)) for x, y, z in item[1])])



    for s in sums:
        print(s)

但是,我收到以下错误:

TypeError: map() must have at least two arguments.

我不知道如何解决它,我不确定 map 是否是完成任务的正确工具。

据我了解,您需要压缩列表中的子列表并将它们加起来:

>>> sums = [(key, [sum(value) for value in zip(*values)]) for key, values in products]
>>> for s in sums:
...     print(s)
... 
('blue', [6, 12, 6, 12, 6, 12, 6, 12, 6, 12])
('yellow', [3, 9, 3, 9, 3, 9, 3, 9, 3, 9])
('red', [11, 12, 7, 14, 11, 4, 7, 6, 8, 6])

作为@alecxe 答案的替代方案,请考虑以下使用 map 和一个不错的列表文字解包:

res = [(k, [*map(sum, zip(*v))]) for k, v in products]

这产生:

[('blue', [6, 12, 6, 12, 6, 12, 6, 12, 6, 12]),
 ('yellow', [3, 9, 3, 9, 3, 9, 3, 9, 3, 9]),
 ('red', [11, 12, 7, 14, 11, 4, 7, 6, 8, 6])]

这稍微快一些,但由于文字解压需要 Python >= 3.5。如果在早期版本上,您必须将其包装在 list 调用中以解压缩 map 迭代器:

res = [(k, list(map(sum, zip(*v)))) for k, v in products]