From 5d1a2bbe541d1673eecfd122df0976b454a480b2 Mon Sep 17 00:00:00 2001 From: XANTRONIX Industrial Date: Thu, 6 Mar 2025 09:40:44 -0500 Subject: [PATCH] Implement nearest() in list.py --- lib/xmet/list.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 lib/xmet/list.py 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