Nathan Miller
Nathan Miller

Reputation: 33

How to find the positions that a number falls in-between in a set of sequential integers

I have an number variable and a sequential set of integers. As the number variable changes, I need to record what positions in the sequential set of integers the number variable falls in-between.

For example, if my set of integers is:

li = [20,21,22,23,24,25,26,27,28,29,30]

And the number variable is:

num = 22.74

My desired output would be the positions in the list that num falls in-between:

2,3

I know that li.index(22) would return the position but only if that exact item is in the list. Haven't been able to come across how to find positions if the number falls in-between items.

I imagine a for loop that compares num to each of the neighboring position items in the list by checking if num falls in between those two integers could solve it. Something like:

for x and the following item in li:
    if x < num < the following item:
        positionone = x
        positiontwo = the following item
    else:
        continue

Just haven't been able to complete how this works, mainly by getting the next position of the list in replace of "the following item". Maybe there's a better way of figuring this? Any help is much appreciated! Thanks!

Upvotes: 1

Views: 856

Answers (4)

Flavio Moraes
Flavio Moraes

Reputation: 1351

A mathematical way of doing this is to subtract the number from the list and check for the two minimum absolute value. If li is a numpy array you can do this in one line:

li = np.array(li)
interval = sorted(np.abs(li-num).argsort()[:2])

Upvotes: 1

Olvin Roght
Olvin Roght

Reputation: 7812

You can compare your number with each element and break loop if current element greater than your number:

for index, current_element in enumerate(li):
    if current_element > num:
        position_one, position_two = index - 1 if index else index, index
        break
else:
    position_one, position_two = index, index

Upvotes: 1

orlp
orlp

Reputation: 117771

I'm not going to touch the ambiguities regarding num being integer or not. But I will give you the tool needed to answer your question.

Let lo and hi respectively be the lower and upper limit of your interest. Then, to get the indices of the number between these two is done as such:

indices = [i for i, x in enumerate(li)
           if lo < x < hi]

Please note that I use < twice there. If you want the lower bound to be inclusive, replace the first < with <=. Similarly, if you want the upper bound to be inclusive replace the second < with <=. Or very generally:

def indices_between(iterable, lo, hi, include_lo=False, include_hi=False):
    lo_req = (lambda x: lo <= x) if include_lo else (lambda x: lo < x)
    hi_req = (lambda x: x <= hi) if include_hi else (lambda x: x < hi)
    return [i for i, x in enumerate(iterable)
            if lo_req(x) and hi_req(x)]

Upvotes: 1

Prune
Prune

Reputation: 77860

You are close with your basic logic. You need to iterate through the list. If you do it using the index, rather than the value, you'll be able to retrieve the positions you want. Your basic logic is fine.

for idx in range(len(li)-1):
    if li[idx] < num < li[idx+1]:
        pos1, pos2 = idx, idx+1

Given that, I expect that you can solve the problem of equality as a special case, right?

Upvotes: 1

Related Questions