 # Find a value between 2 values in a list

I am trying to find the the levels up and down for an element by its elevation. I would like to find, between which levels that element is.
For example:

Elevations = [3.5,4,10]
Levels = [0,3,6,9,12]

Expected Output = [[3,6], [3,6], [9,12]]

thank you very much!

@Deniz_Maral Try this: ``````def numBetween(num,numList):
if min(numList) <= num <= max(numList):
numList.sort(reverse=True)
lowerNum = min(numList,key=lambda x:x-num>0)
upperNum = numList[numList.index(lowerNum)-1]
return lowerNum,upperNum
else:
return "Number out of range"

toList = lambda x : x if hasattr(x, '__iter__') else [x]

OUT = [numBetween(x, IN) for x in toList(IN)]
``````
4 Likes

Thanks! Great work!
It should work like that! I will inform you on monday 1 Like

Nodes…

Design Script …

`List.GetItemAtIndex(b,List.Transpose([List.IndexOf((a<1>-b > 0)<1>,false)-1,List.IndexOf((b-a<1> > 0)<1>,true)]));`

within.dyn (27.4 KB)

4 Likes

Would it be too complicated if list of levels is not sorted? I have sorted the list so as to make calculation easier. After finding the elevation of levels I would do like that: Level.ByElevation > atIndex  and then assign the offset to element…

I have to create a level between these two levels and if there are more elements with different elevations, I have to take the smallest elevation of element between these levels and place with offset to new level.

I think that sorting the list, then asking for the first elevation that is more than the target be best. The first true would be the level above, and the prior index would be the left below.

1 Like