-1

I have a list:

[5.130, 9.345, 0.134, -7.234, 4.321, -46.321]

and I try to get this list:

[5.130, 14.475, 14.609, 7.372,  11.693, -34.628]

that is:

5.130+9.345=14.475
14.475+0.134=14.609
14.609+(-7.234)=7.372
7.372+4.321=11.693
11.693+(-46.321)=-34.628
Cow
  • 2,543
  • 4
  • 13
  • 25
shoit
  • 7
  • 2

2 Answers2

0
result = ['{:0.3f}'.format(sum(lst[:i+1])) for i in range(len(lst))]
print(result)

Output:

['5.130', '14.475', '14.609', '7.375', '11.696', '-34.625']

I'm not getting the same results as you but I think your calculations are wrong (14.609+(-7.234)=7.375).

Joan Lara
  • 1,362
  • 8
  • 15
0

So you need to make a list of cumulative sums. You can do it by using for loop and .append() method

a = [5.130, 9.345, 0.134, -7.234, 4.321, -46.321]

cumulative_sum = []
new_sum = 0
for i in a:
    new_sum += i
    cumulative_sum.append(new_sum)
print(cumulative_sum)
Shounak Das
  • 350
  • 12