Python series algorithm -


i have list of sort :

numbers = [12, none, none, 17, 20, none, 28] 

what's best path consider in order fill in none values appropriate numbers none values numbers in between defined values.

example :

numbers = [12, **14**, **16**, 17, 20, **24**, 28] 

interpolating on fly create linear interpolation:

def interpolate(l):     last = none     count = 0     num in l:         if num none:             # count number of gaps             count += 1             continue         if count:             # fill gap based on last seen value , current             step = (num - last) / float(count + 1)             in xrange(count):                 yield last + int(round((1 + i) * step))             count = 0         last = num         yield num 

demo:

>>> list(interpolate([12, none, none, 17, 20, none, 28])) [12, 14, 15, 17, 20, 24, 28] 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -