Dini KF - 1 year ago 52

Python Question

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]]

each list on both lists consist of

`[point name, x value, y value]`

So, I have to assign score for every point in S list refer to R list, by comparing y value of lists in list S to the closest and smaller y value in list R.

the score would be:

`(1/y value of list S) over (1/closest and smaller y value of list R)`

example: point

`[B, 0.68, 0.39]`

`[C, 2.00, 0.13]`

`(1/0.39)/(1/0.13)`

I need the result to be like this:

`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]]`

This is what i did so far, but I don't know how to make S_score list included the point name. Beside this code seem not efficient, since if there is much more member of list R as reference, I need to make condition as much as that

`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

Thank you in advance

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**