Implement nearest() in list.py

This commit is contained in:
XANTRONIX 2025-03-06 09:40:44 -05:00
parent e18ca9be1f
commit 5d1a2bbe54

12
lib/xmet/list.py Normal file
View file

@ -0,0 +1,12 @@
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