I have an array of numbers and I am tryin to find the cumulative some from the previous number to the next. See details below please

Let's say my array is:

x = [12, 8, 8, 6]

As you can see the first number is 12 so the sum of this and the next number which is 8 is 20. Next, the sum of 20 and 8 is 28, so on.

What I want as the final output is:

(12, 20), (20,28), (28, 34)


from itertools import chain
x = [12,8,8]
it = chain(x,[0])
result = list(x + next(it) for x in it)
print ('\n',result,'\n')


Read more here: https://stackoverflow.com/questions/66995104/i-have-an-array-of-numbers-and-i-am-tryin-to-find-the-cumulative-some-from-the-p

Content Attribution

This content was originally published by user11014777 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: