我有一个巨大的子列表,每个子列表由一个元组和一个包含4个整数的列表组成。
我想创建一个唯一元组的列表,它将列表中的每个整数值相加(保持列表中的四个整数分开)。
短的例子:
[[(30, 40), [4, 7, 7, 1]],[(30, 40), [2, 9, 3, 4]],[(30, 40), [6, 5, 10, 0]],[(20, 40), [4, 0, 4, 0]],[(20, 40), [3, 4, 14, 5]],[(20, 40), [3, 2, 12, 0]],[(10, 40), [223, 22, 12, 9]]]
输出想要的:
[[(30, 40), [12, 21, 20, 5]],[(20, 40), [2, 9, 3, 4]],[(10, 40), [223, 22, 12, 9]]
我试过用字典
l = [[(30, 40), [4, 7, 7, 1]],[(30, 40), [2, 9, 3, 4]],[(30, 40), [6, 5, 10, 0]],[(20, 40), [4, 0, 4, 0]],[(20, 40), [3, 4, 14, 5]],[(20, 40), [3, 2, 12, 0]],[(10, 40), [223, 22, 12, 9]]]
dict_tuples = {}
for item in l:
if item[0] in dict_tuples:
dict_tuples[item[0]] += item[1]
else:
dict_tuples[item[0]] = item[1]
但是这里我只是得到了每个元组的整数值的长列表。我想对四个整数列表中的每个索引求和。
可以创建一个字典,其中键是第一个元组,值是子列表的列表。在第二步中,对每个索引的值求和:
lst = [
[(30, 40), [4, 7, 7, 1]],
[(30, 40), [2, 9, 3, 4]],
[(30, 40), [6, 5, 10, 0]],
[(20, 40), [4, 0, 4, 0]],
[(20, 40), [3, 4, 14, 5]],
[(20, 40), [3, 2, 12, 0]],
[(10, 40), [223, 22, 12, 9]],
]
out = {}
for t, l in lst:
out.setdefault(t, []).append(l)
out = [[k, [sum(t) for t in zip(*v)]] for k, v in out.items()]
print(out)
打印:
[
[(30, 40), [12, 21, 20, 5]],
[(20, 40), [10, 6, 30, 5]],
[(10, 40), [223, 22, 12, 9]],
]
itertools.groupby
使这变得微不足道。这可以一次完成,但是为了查看转换的每个步骤:
from itertools import groupby
from operator import itemgetter
l = [[(30, 40), [4, 7, 7, 1]], [(30, 40), [2, 9, 3, 4]], [(30, 40), [6, 5, 10, 0]], [(20, 40), [4, 0, 4, 0]], [(20, 40), [3, 4, 14, 5]], [(20, 40), [3, 2, 12, 0]], [(10, 40), [223, 22, 12, 9]]]
s = sorted(l, key=itemgetter(0))
# [[(10, 40), [223, 22, 12, 9]], [(20, 40), [4, 0, 4, 0]], [(20, 40), [3, 4, 14, 5]], [(20, 40), [3, 2, 12, 0]], [(30, 40), [4, 7, 7, 1]], [(30, 40), [2, 9, 3, 4]], [(30, 40), [6, 5, 10, 0]]]
g = groupby(s, key=itemgetter(0))
l2 = [(k, [x[1] for x in v]) for k, v in g]
# [((10, 40), [[223, 22, 12, 9]]), ((20, 40), [[4, 0, 4, 0], [3, 4, 14, 5], [3, 2, 12, 0]]), ((30, 40), [[4, 7, 7, 1], [2, 9, 3, 4], [6, 5, 10, 0]])]
l3 = [(k, list(zip(*v))) for k, v in l2]
# [((10, 40), [(223,), (22,), (12,), (9,)]), ((20, 40), [(4, 3, 3), (0, 4, 2), (4, 14, 12), (0, 5, 0)]), ((30, 40), [(4, 2, 6), (7, 9, 5), (7, 3, 10), (1, 4, 0)])]
l4 = [(k, [sum(x) for x in v]) for k, v in l3]
# [((10, 40), [223, 22, 12, 9]), ((20, 40), [10, 6, 30, 5]), ((30, 40), [12, 21, 20, 5])]