0

Hi im a beginner in python and need some advice with this code. I want to get the sum of the previous values

D = {1:[90,3,'a'], 2:[270,2,'b'], 3:[30,2,'c']}
n = 0
xend = 0
for d in D.values():
    n += 1
    if n == 1:
       xstart = 0
       xend += d[0]
    xstart += xend

I want to get the value of xstart for each iteration as the sum of the previous values in list index 0. for eg, when d = [90,3,'a'], xstart = 0. when d = [270,2,'b'], xstart = 90. when d = [30,2,'c'], xstart = 360. and so on. Help would be appreciated!

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Cel
  • 1

1 Answers1

0

Here is a small snippet with the minimal code to get your cumulative sum, if you want xstart to be the previous sum, then use the value before xstart += d[0]:

D = {1:[90,3,'a'], 2:[270,2,'b'], 3:[30,2,'c']}

xstart = 0
for d in D.values():
    print(f'd = {d} ; xstart = {xstart}')
    xstart += d[0]
    print(f'now xstart = {xstart}')

output:

d = [90, 3, 'a'] ; xstart = 0
now xstart = 90
d = [270, 2, 'b'] ; xstart = 90
now xstart = 360
d = [30, 2, 'c'] ; xstart = 360
now xstart = 390
mozway
  • 194,879
  • 13
  • 39
  • 75