13 lines
352 B
Python
13 lines
352 B
Python
|
|
def nearest(a: list, b: list) -> list:
|
||
|
|
"""
|
||
|
|
For each item in list a, find the nearest value in list b, and return a
|
||
|
|
list of tuples with these associations.
|
||
|
|
"""
|
||
|
|
ret = list()
|
||
|
|
|
||
|
|
for item in a:
|
||
|
|
candidates = sorted([(v, abs(item-v)) for v in b], key=lambda t: t[1])
|
||
|
|
ret.append((item, candidates[0][0]))
|
||
|
|
|
||
|
|
return ret
|