diff --git a/lib/xmet/list.py b/lib/xmet/list.py new file mode 100644 index 0000000..95d099f --- /dev/null +++ b/lib/xmet/list.py @@ -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