[Solved] python, 2 operations: find closest value above and below a specified value in array, find associated values in another array [closed]


If you have list of numbers:

In [128]: L
Out[128]: [9, 8, 7, 6, 5, 4, 3, 2, 1]

sort them first:

In [129]: L.sort()    
In [130]: L
Out[130]: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Then do the check:

In [131]: for lower, upper in zip(L[:-1], L[1:]):
   .....:     if lower <= 2.44 <= upper:
   .....:         print(lower, upper)
   .....:         break
   .....:         
2 3

Adjust the if lower <= 2.44 <= upper to reflect if you want upper and lower boundaries inclusive or exclusive.

Please try yourself for the second part.

2

solved python, 2 operations: find closest value above and below a specified value in array, find associated values in another array [closed]