На 3ем питоне можно было бы лаконично:
Код:
from itertools import accumulate, zip_longest
a = (2, 4, 6, 2, 10)
print(*accumulate(map(sum, zip_longest(*[iter(a)] * 2, fillvalue = 0))))
А на 2ом придется чуть больше написать:
Код:
def my_accum(iterable):
it = iter(iterable)
s = 0
while True:
try:
a = next(it)
except StopIteration:
return
try:
b = next(it)
except StopIteration:
yield s + a
return
s += a + b
yield s
a = (2, 4, 6, 2, 10)
print(list(my_accum(a)))