I've found this script for linear interpolation in Python on Stack Overflow. It works perfectly well in Python 2.7, but fails in newer versions.
Here is the code:
from bisect import bisect_left class Interpolate(object): def __init__(self, x_list, y_list): if any(y - x <= 0 for x, y in zip(x_list, x_list[1:])): raise ValueError("x_list must be in strictly ascending order!") x_list = self.x_list = map(float, x_list) y_list = self.y_list = map(float, y_list) intervals = zip(x_list, x_list[1:], y_list, y_list[1:]) self.slopes = [(y2 - y1)/(x2 - x1) for x1, x2, y1, y2 in intervals] def __getitem__(self, x): i = bisect_left(self.x_list, x) - 1 return self.y_list[i] + self.slopes[i] * (x - self.x_list[i]) i = Interpolate ([0,2,3], [9,5,8]) y = i[1] This is the error I’m getting:
TypeError: 'Interpolate' object does not support indexing. What has been changed that means the code won’t work anymore?
intervals = zip(x_list, x_list[1:], y_list, y_list[1:]), I haveTypeError: 'map' object is not subscriptable. // Oh wait, I see.