Suppose I have a certain list x with numbers, and another list y with other numbers. Elements of y should be elements of x, but due to noise in measurements, they are kind of different. I want to find, for each value of y, the value of x that is the nearest to it.
I can do this with some loops and check, for each element y[i], which element x[j] minimizes abs(x[j]-y[i]), but I'm pretty sure there is a much easier, cleaner way to do this. The lists could be huge so I'm looking for efficient code here.
The code I've written so far is:
x_in = [1.1, 2.2, 3, 4, 6.2]
y_in = [0.9, 2, 1.9, 6, 5, 6, 6.2, 0.5, 0, 3.1]
desired_output = [1.1, 2.2, 2.2, 6.2, 4, 6.2, 6.2, 1.1, 1.1, 3]
y_out = []
for y in y_in:
aux = [abs(l - y) for l in x_in]
mn,idx = min( (aux[i],i) for i in range(len(aux)) )
y_out.append(x_in[idx])
>>> y_out == desired_output
True
But I don't know if there is a more efficient way to do this...
EDIT:
Due to my ignorance, I forgot to clarify something that may be of relevance according to the comments I've recieved.
- The
xlist is sorted. xis the only list that can have a pretty big size: between 500,000 and 1,000,000 elements, in general.ywill in general be really small, less than 10 elements.
Given that
xis sorted, the most efficient way to do this is usingbisectto search for the closest value. Just create a list of mid points between the x values and run bisect on those:This will run in
O(Mlog(N)+N)time where `M=len(y), N=len(x)(For python2 do
from __future__ import divisionor usefloat(x1+x2)/2in themid_pointscalculation)