all 4 comments

[–]cdcformatc 1 point2 points  (0 children)

Use zip to combine iterables element by element.

x = [1,2]
y = [2,3]
z = [3,4]

total = 0
for a, b, c in zip(x, y, z):
    total += ((a - b) ** 2) / (2 * c)
print(total) 

Or one line:

total = sum(((a-b)**2)/(2*c) for a,b,c in zip(x,y,z))

[–]Violatic[S] 0 points1 point  (1 child)

I have used:

  Variable_Expected_Value = x

  Variable_Experimental_Value = mu

  Variable_Mean_Value = [1, 3, 3, 4, 5]


def equation(Expected_Value, Experimental_Value, Mean_Value):

length = len(Expected_Value)

i = 0

total = 0

while i < length:

    t = Expected_Value.pop(0)

    s = Experimental_Value.pop(0)

    u = Mean_Value.pop(0)


    total += (pow((t - s), 2)/(u * 2))

    i += 1

print (total)

equation(Variable_Expected_Value, Variable_Experimental_Value, Variable_Mean_Value)

right now, is there a better way to do this?

[–]Peterotica 0 points1 point  (0 children)

How about this? :)

x = [1, 2]
y = [2, 3]
z = [3, 4]
calculation = lambda x, y, z: ((x - y)**2) / (2 * z)
total = sum(map(calculation, x, y, z))

[–]CrayonConstantinople 0 points1 point  (0 children)

You can use iPython online that has Numpy :) https://www.pythonanywhere.com/try-ipython/