Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting a boundary of an item from another list

Tags:

python

I have a list which is like,

tlist = [0.0, 0.07, 0.13, 0.15, 0.2, 0.22] (which is sorted)

I have another list which is,

newlist = [0.0, 0.04, 0.08, 0.12, 0.16, 0.2] (numbers with a difference of 0.04)

I have to go through each item of the second list and check in which boundary (between which two numbers from the tlist) the number lies.

Like if I am checking for first item which is '0.0' from the second list then it falls between '0.0' and '0.07' in the first list.

Similarly, the next item in the second list which is '0.04' falls between '0.0' and '0.07' again in the first file.

So, for every item checked from the second list it should know its boundary. And it should set the boundaries. The result could be like, the range for '0.0' is x to y where x = 0.0 and y = 0.07.

If there is a number in tlist which is exactly the same as one of the numbers from the newlist then the program should neglect it or it can print a statement like "no boundary possible" and continue with the next number.

How do I put this into code. Thank you.

like image 277
zingy Avatar asked Dec 09 '22 06:12

zingy


1 Answers

Simple approach using enumerate:

for n in newlist:
    for i, t in enumerate(tlist):
        if t > n:
            # found upper boundry, previous must be equal or lower
            upper, lower = t, tlist[i-1]
            break
    print lower, n, upper
like image 123
zeekay Avatar answered Dec 27 '22 09:12

zeekay