Implement nearest() in list.py
This commit is contained in:
parent
e18ca9be1f
commit
5d1a2bbe54
1 changed files with 12 additions and 0 deletions
12
lib/xmet/list.py
Normal file
12
lib/xmet/list.py
Normal 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
|
Loading…
Add table
Reference in a new issue