Given a list, how can I find sums of part of the list? For example:
a = [2,5,4,6,8]
sum(a) = 25
I want to find out if a part of the list sums up to a certain number.
The list part should have a certain length. This is my goal:
ex_list = [2,5,4,6,8]
dif_list = partsum(ex_list, 3)
print(dif_list) ==> [11, 13, 15, 12, 14, 16, 15, 17, 19, 18]
Each element in 'dif_list' is given by taking 3 numbers out of 'ex_list' and summing them up, i.e. 2+5+4 = 11, 2+5+6 = 13, 2+5+8 = 15, 2+4+6 = 12, etc.
Also for reference:
ex_list = [2,5,4,6,8]
another_list = partsum(ex_list, 4)
print(another_list) ==> [17, 19, 21, 20, 23]
Because 2+5+4+6 = 17, 2+5+4+8 = 19, etc.
Basically, partsum(thing, num) will take 'num' items of 'thing', sum them up, and append it to a new list. All help is appreciated, thanks!
numelements and their sum?