-4

I am writing an algorithm in Python to fit a data (I need to write my own algorithm). But I have a problem with the arrays. For example:

import math
x=[2,1]
a=sum([x[i]*x[i] for i in range(len(x))])

It is working. However when I tried to divide, it is not working.

import math
x=[2,1,5]
y=[3,2,5]
a=sum(([y[i]*y[i] for i in range(len(x))])/([x[i]*x[i] for i in range(len(x))]))

How can I fix it? Do you have any idea?

Fiber
  • 11
  • 2

1 Answers1

0

Looking on your code I believe that your goal is to divide every element of the y list by every element of the x list and then sum them, if that is the case, I would write something like that:

import math
x=[2,1,5]
y=[3,2,5]

a = sum([yy / xx for yy, xx in zip(y, x)])

You just iterate throught the zipped lists and divide each element from y by corresponding element from x. In your code you are trying to divide list by a list, which is not a supported operation, so thats why you get the error.

Mariusz Ch.
  • 51
  • 1
  • 5