使用 python.itertools 对多个列表中的插槽求和
using python.itertools to sum slots across multiple lists
我有这个:
[
[ [1,1,1,0,0,0,0], [0,1,2,3,4,5,6], [1,2,3,4,5,6,7], [2,3,4,5,6,7,8], [3,4,5,6,7,8,9], [4,5,6,7,8,9,0] ]
, [ [2,2,2,0,0,0,0], [0,1,2,3,4,5,6], [1,2,3,4,5,6,7], [2,3,4,5,6,7,8], [3,4,5,6,7,8,9], [4,5,6,7,8,9,2] ]
, [ [3,3,3,0,0,0,0], [0,1,2,3,4,5,6], [1,2,3,4,5,6,7], [2,3,4,5,6,7,8], [3,4,5,6,7,8,9], [4,5,6,7,8,9,0] ]
]
我正在尝试得到这个:
[
[[6, 6, 6, 0, 0, 0, 0], [0, 3, 6, 9, 12, 15, 18], [3, 6, 9, 12, 15, 18, 21], [6, 9, 12, 15, 18, 21, 24], [9, 12, 15, 18, 21, 24, 27], [12, 15, 18, 21, 24, 27, 2]]
]
它只是最深列表中所有相对位置的总和。
现在,我有 3 个嵌套的 for 循环,我希望你们中的一位 itertools 专家可以向我展示一种更快、更优雅的方法来做到这一点
你不需要 itertools,你可以使用列表推导和 zip(*)
:
>>> lst = [[[1, 1, 1, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 0]], [[2, 2, 2, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 2]], [[3, 3, 3, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 0]]]
>>> [[sum(y) for y in zip(*x)] for x in zip(*lst)]
[[6, 6, 6, 0, 0, 0, 0], [0, 3, 6, 9, 12, 15, 18], [3, 6, 9, 12, 15, 18, 21], [6, 9, 12, 15, 18, 21, 24], [9, 12, 15, 18, 21, 24, 27], [12, 15, 18, 21, 24, 27, 2]]
请注意,您当前的预期输出是一个包含列表列表的列表,这是不必要的,最好将其保留为列表列表。
另一种没有列表理解的解决方案:
# In Python 2.x
>>> map(lambda l: map(sum, zip(*l)), zip(*lst))
# In Python 3.x
>>> list(map(lambda l: list(map(sum, zip(*l))), zip(*lst)))
请注意,此解决方案只是概念验证,您应该使用@AshwiniChaudhary 提供的解决方案,因为它更清晰易读。
我有这个:
[
[ [1,1,1,0,0,0,0], [0,1,2,3,4,5,6], [1,2,3,4,5,6,7], [2,3,4,5,6,7,8], [3,4,5,6,7,8,9], [4,5,6,7,8,9,0] ]
, [ [2,2,2,0,0,0,0], [0,1,2,3,4,5,6], [1,2,3,4,5,6,7], [2,3,4,5,6,7,8], [3,4,5,6,7,8,9], [4,5,6,7,8,9,2] ]
, [ [3,3,3,0,0,0,0], [0,1,2,3,4,5,6], [1,2,3,4,5,6,7], [2,3,4,5,6,7,8], [3,4,5,6,7,8,9], [4,5,6,7,8,9,0] ]
]
我正在尝试得到这个:
[
[[6, 6, 6, 0, 0, 0, 0], [0, 3, 6, 9, 12, 15, 18], [3, 6, 9, 12, 15, 18, 21], [6, 9, 12, 15, 18, 21, 24], [9, 12, 15, 18, 21, 24, 27], [12, 15, 18, 21, 24, 27, 2]]
]
它只是最深列表中所有相对位置的总和。 现在,我有 3 个嵌套的 for 循环,我希望你们中的一位 itertools 专家可以向我展示一种更快、更优雅的方法来做到这一点
你不需要 itertools,你可以使用列表推导和 zip(*)
:
>>> lst = [[[1, 1, 1, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 0]], [[2, 2, 2, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 2]], [[3, 3, 3, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 0]]]
>>> [[sum(y) for y in zip(*x)] for x in zip(*lst)]
[[6, 6, 6, 0, 0, 0, 0], [0, 3, 6, 9, 12, 15, 18], [3, 6, 9, 12, 15, 18, 21], [6, 9, 12, 15, 18, 21, 24], [9, 12, 15, 18, 21, 24, 27], [12, 15, 18, 21, 24, 27, 2]]
请注意,您当前的预期输出是一个包含列表列表的列表,这是不必要的,最好将其保留为列表列表。
另一种没有列表理解的解决方案:
# In Python 2.x
>>> map(lambda l: map(sum, zip(*l)), zip(*lst))
# In Python 3.x
>>> list(map(lambda l: list(map(sum, zip(*l))), zip(*lst)))
请注意,此解决方案只是概念验证,您应该使用@AshwiniChaudhary 提供的解决方案,因为它更清晰易读。