Help with calling data and sorting a list with respect to another object
I am doing approximation of a set of data points using multiple methods. So I input the data in the __main__ section and then call it up in my individual modules. But in one of my programs, I extend the list to use one method. But then after that program runs, my xData has been expanded, and the array won't work for my other method. Even if I say z = xData or something in the program, my xData list still gets changed. Is there a way to input my data and then us it in a program under some other name or variable and have it remain unchanged in the __main__ section to be used in other modules?? Also, is there an easy way to sort arrays of values with respect to another value? I need to sort my xData list in order of distance from 10. That is xData = [0,3,5,8,13], could python evaluate abs(0-10) = 10 ... etc... abs (13-10) = 3 and sort the list [8,13,5,3,0]? Without it being an enormous tedious program like I was coming up with?
Here is an example of my code. As you can see, I had to put the same values in as xData, xData2, and xData3 because I am trying to run Hermite interpolation once, then Lagrange interpolation twice for two different sets, yData and sData.
def lagrange(x, xn, yn,n): l = 0.0 L = [] for i in range(1,n): s = 1.0 for j in range(n): if i != j: s *= ((x - xn[j])/(xn[i] - xn[j])) l += s*yn[i] L.append(l) return L
if __name__ == '__main__':
method = 'Lagrange'
xData = [0.0, 3.0, 5.0, 8.0, 13.0] yData = [0.0, 225.0, 383.0, 623.0, 993.0] xData2 = [0.0, 3.0, 5.0, 8.0, 13.0] xData3 = [0.0, 3.0, 5.0, 8.0, 13.0] yData2 = [0.0, 225.0, 383.0, 623.0, 993.0] sData = [75.0, 77.0, 80.0, 74.0, 72.0] sData2 = [75.0, 77.0, 80.0, 74.0, 72.0] x = 10
if method == 'Lagrange': print print "ith degree Lagrange Interpolation polynomials:" print "Distance:" for i in range(len(xData2)-2): Ld = lagrange(x, xData2, yData2, len(xData2)) print "P_i(x): ", Ld[-1], "i =", len(xData2)-1 xData2.pop(-1) print "Speed:" for i in range(len(xData3)-2): Ls = lagrange(x, xData3, sData2, len(xData3)) print "P_i(x): ", Ls[-1], "i = ", len(xData3)-1 xData3.pop(-1)