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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With