Gabriel
Gabriel

Reputation: 42389

Check between which floats in a list a given float falls

I have a list that looks like this:

# Ordered list.
a = [0.1, 0.3, 0.4, 0.5, 0.6, 0.7, 0.9]

I need to iterate through a list of floats that looks like this:

# Not ordered list.
b = [0.12, 0.53, 0.30, 0.03, 0.77, 0.62, 0.98, 0.01, 0.42, 0.33, 1.3]

check between which elements in list a each element in list b falls, and return the index of the element in a (to the right). For example, for the list b above the results would look like:

indxs = [1, 4, 2, 0, 6, 5, 7, 0, 3, 2, 7]

(notice the index 7 which points to an extra element in a associated to > max(a))

I could do this with a rather elaborated for/if loop as shown below, but I was wondering if there might be an easier way with some function I might not be aware of (numpy/scipy functions are welcomed)

MWE:

indxs = []
for b_el in b:
    # Check if float is to the right of max(a)
    if b_el > max(a):
        indxs.append(len(a))
    else:
        for a_el in a:
            if b_el < a_el:
                indxs.append(a.index(a_el))
                break
            elif b_el == a_el:
                indxs.append(a.index(a_el) + 1)
                break

Upvotes: 2

Views: 171

Answers (2)

mdurant
mdurant

Reputation: 28683

Here is an expression:

where(b<=a.max(),argmax((b[:,None] - a) < 0,axis=1),len(a))

(if a and b are arrays)

Upvotes: 1

user2357112
user2357112

Reputation: 281519

The ordinary Python solution is the bisect module, which has several functions for binary search:

>>> [bisect.bisect(a, x) for x in b]
[1, 4, 2, 0, 6, 5, 7, 0, 3, 2, 7]

The NumPy solution is numpy.searchsorted, which does much the same:

>>> numpy.searchsorted(a, b, side='right')
array([1, 4, 2, 0, 6, 5, 7, 0, 3, 2, 7], dtype=int64)

Upvotes: 8

Related Questions