Iterating over a two lists of integers in python? -
i'm coding function produce
, being
expression.
given 2 lists: xs , ys , x wrote algorithm below:
#!/usr/bin/env python #coding: utf8 sympy import * numpy import * import json l = {} x = symbol('x') expresion = '' def lagrange(xs, ys, x): k in len(xs): if k != 0 or k != len(xs)-1: #si no es el primero o el último término de la sumatoria expresion = expresion + '+' + ys[k] + '*' elif k==0: expresion = expresion + ys[k] + '*' in len(xs): if k==i: continue # si i==k saltamos esta iteración para eliminar división sobre cero expresion = expresion + '(' + '(' + x + '-' + xs[i] +' )' + '/' + '(' + xs[k] + '-' + xs[i] + ')' +')' print simpify(expresion) when ran:
#!/usr/bin/env python #coding: utf8 lagrange import * lagrange([0,1,2,4],[-1,0,7,4,63],3) i got:
[......]line 12, in lagrange k in len(xs): typeerror: 'int' object not iterable [finished in 0.5s exit code 1] so how should iterate on xs , ys elements , include condition if i=k continue on loop?
for k in range(len(xs)) will give possible indicies in xs.
len(xs) returns int, not iterable. want same i loop.
Comments
Post a Comment