I have 2 arrays list1
and list2
list1 = np.array([1052, 1092, 1117,1135, 1156,1212,1249,1325,1363,1380,1382,1400,1484])
list2 = np.array([1053.435, 1093.698, 1118.342, 1136.697,
1157.832, 1212.938, 1215.741, 1250.266, 1252.352,
1254.453, 1256.831, 1259.182, 1326, 1367.228,
1400, 1410, 1740])
for each element in list1
I would like to find the closest element in list2
and return its index.
mylist = []
for idxlabel in range(0,len(list1)):
a = min(enumerate(list2), key=lambda x:abs(x[1]-list1[idxlabel]))
print a
mylist.append(np.copy(a))
My problem is that after that one element in list2
is found as "best match" I would like to remove it from the search to avoid that different element in list1
match with the same element in list2
(0, 1053.4349999999999)
(1, 1093.6980000000001)
(2, 1118.3420000000001)
(3, 1136.6969999999999)
(4, 1157.8320000000001)
(5, 1212.9380000000001)
(7, 1250.2660000000001)
(12, 1326.0)
(13, 1367.2280000000001)
(13, 1367.2280000000001)
(13, 1367.2280000000001)
(14, 1400.0)
(15, 1410.0)
In this example indeed the the 9th 10th and 11th elements of list1 match all with the 13th element of list2
and this is NOT desired...