class InterpolatedArray(object):
"""An array-like object that provides
interpolated values between set points."""
def __init__(self, points):
self.points = sorted(points)
def __getitem__(self, x):
if x > self.points[-1][0]:
return self.points[-1][1] + (x - self.points[-1][0]) * float((self.points[-1][1] - self.points[-2][1]) / (self.points[-1][0] - self.points[-2][0]))
elif x < self.points[0][0]:
return self.points[0][1] + (x - self.points[0][0]) * float(self.points[1][1] - self.points[0][1] / (self.points[1][0] - self.points[0][0])
else:
lower_point, upper_point = self._GetBoundingPoints(x)
return self._Interpolate(x, lower_point, upper_point)
def _GetBoundingPoints(self, x):
"""Get the lower/upper points that bound x."""
lower_point = None
upper_point = self.points[0]
for point in self.points[1:]:
lower_point = upper_point
upper_point = point
if x <= upper_point[0]:
break
return lower_point, upper_point
def _Interpolate(self, x, lower_point, upper_point):
"""Interpolate a Y value for x given lower & upper
bounding points."""
slope = (float(upper_point[1] - lower_point[1]) /
(upper_point[0] - lower_point[0]))
return lower_point[1] + (slope * (x - lower_point[0]))
它只是抱怨行中的“无效语法”: 否则:
答案 0 :(得分:2)
你错过了一个封闭的括号:
return self.points[0][1] + (x - self.points[0][0]) * float(self.points[1][1] - self.points[0][1] / (self.points[1][0] - self.points[0][0])
您的意思是)
之后的self.points[0][1]
。
答案 1 :(得分:0)
这一行需要一个右括号(else
之前的那个):
return self.points[0][1] + (x - self.points[0][0]) * float(self.points[1][1] - self.points[0][1] / (self.points[1][0] - self.points[0][0])
你有3个左括号但只有两个结束。
我无法告诉你确切的位置,因为这取决于你想做什么。但是,它很可能是:
return self.points[0][1] + (x - self.points[0][0]) * float(self.points[1][1] - self.points[0][1] / (self.points[1][0] - self.points[0][0]))
# here--^
或:
return self.points[0][1] + (x - self.points[0][0]) * float(self.points[1][1] - self.points[0][1]) / (self.points[1][0] - self.points[0][0])
# here--^