I am a totally beginner on programming language including python and this problem kind of difficult for me.
Appreciate if you guys can help me.
So I have these two list of list:
S = [[D, 0.67, 0.05], [A, 0.68, 0.06], [C, 2.00, 0.13], [B, 0.68, 0.39], [E, 1.28, 0.97], [F, 0.72, 1.05], [I, 0.58, 1.05], [G, 1.25, 2.03], [H, 1.10, 3.59], [J, 0.98, 4.14]]
R = [[D, 0.67, 0.05], [A, 0.68, 0.06], [C, 2.00, 0.13]]
[point name, x value, y value]
(1/y value of list S) over (1/closest and smaller y value of list R)
[B, 0.68, 0.39]
[C, 2.00, 0.13]
(1/0.39)/(1/0.13)
S_score = [[D,1],[A,1],[C,1],[B,0.33],[E,0.13],[F,0.12],[I,0.12],[G,0.06],[H,0.04],[J,0.03]]
S_score = []
for i in xrange(len(S)):
if S[i][2] >= R[0][2] and S[i][2] <= R[1][2]:
value = (1/S[i][2]) / (1/R[0][2])
score.append(value)
else:
if S[i][2] >= R[1][2] and S[i][2] <= R[2][2]:
value = (1 / S[i][2]) / (1 / R[1][2])
score.append(value)
if S[i][2] >= R[2][2]:
value = (1 / S[i][2]) / (1 / R[2][2])
score.append(value)
print "Score: ", S_score
This works:
def maximum(arr):
x = arr[0]
for x1 in arr:
x = x1 if x1[2] > x[2] else x
return x
def foo(x, arr):
x1 = maximum(filter(lambda x2: x2[2] <= x[2], arr))
return (1/x[2])/(1/x1[2])
result = [[x[0], foo(x, R)] for x in S]
The idea here is that the foo function will send to the maximum function only those values of R that have y lower or equal than the current y, and that one will return the one with the largest y.
After that it's just the calculation you provided.
The code with the result is a simple list comprehension.