我的直觉告诉我reduce
(来自functools import reduce)将提供我想要的东西,但我不能在这里用它.
def test_reduce_lists_by_summing_them():
"""
Sum each item from the first list with the same positional item from the subsequent list.
The result is the next first list and appended to the returned result list.
"""
input_ = [[1, 0, 0], [0, 1, 0], [0, 0, 2], [3, 0, 0]]
expected_output = [[1, 1, 0], [1, 1, 2], [4, 1, 2]]
def f(x, y):
"""Return the sum of two positional items"""
return x + y
output = []
for group in zip(input_[0:], input_[1:]):
# Obvisously not working since the first list is not generated but just taken grp[0]
output.append(list(map(f, group[0], group[1])))
assert output == expected_output