0

On python I have two lists basicaly

xvales = [2, 4, 6, 8, 10, 12]
yvales = [100, 95, 90, 85, 80, 75]
sumation = 0

How can I use a for loop and pull corresponding values from each list to use in a formula.first iteration i=2 and j=100. second iteration i=4 and j=95. third iteration i=6 and j=90. I think you understand what I'm trying to do. I did try to do this.

For i in xvales and j in yvales:    
    v = i **2 / ( j+1 )    
    sumation += v

4 Answers4

2
total = sum(i**2 / (j+1) for i,j in zip(xvalues, yvalues))
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
1

Use zip

>>> zip(xvales, yvales)
[(2, 100), (4, 95), (6, 90), (8, 85), (10, 80), (12, 75)]

Then, loop on it, and sum it:

sumation = sum(i **2 / ( j+1 ) for i, j in zip(xvales, yvales))

Edit

However, you probably want a float division, else this results in 2:

sumation = sum(i **2 / float( j+1 ) for i, j in zip(xvales, yvales))
# 4.475365812518561
njzk2
  • 38,969
  • 7
  • 69
  • 107
0

Use the zip function to iterate over both at once.

Maintaining the rest of your structure:

for i, j in zip(xvales, yvales):
    v = i **2 / ( j+1 )    
    sumation += v
mhlester
  • 22,781
  • 10
  • 52
  • 75
0

I think this does it

xvales = [2, 4, 6, 8, 10, 12]
yvales = [100, 95, 90, 85, 80, 75]
summation = sum([i ** 2 / (j+1) for i,j in zip(xvales, yvales)])
bnjmn
  • 4,508
  • 4
  • 37
  • 52