I have a list of values, let them be
a_list_sort = [2, 3, 4, 4.1, 5, 5.05, 2.1, 3.05, 7, 2.2, 3.2]
My goal is to get a list present_a
with 'unique' values from a_list_sort
, and by 'unique' I mean following:
If the n-th value from a_list_sort
is close to one of the values already present in present_a
, it is not 'unique', otherwise it is and it is added to present_a
.
I tried this code:
angle_th = 0.1
present_a = [a_list_sort[0]] # Because the first value in a_list_sort is 'unique' a priori
present_a_idxs = [] # List of indexes
for a_idx in range(1, len(a_list_sort)):
if np.any(np.abs(np.asarray(present_a) - a_list_sort[a_idx]) < angle_th): # Is not 'unique'
pass
else:
present_a.append(a_list_sort[a_idx])
present_a_ind.append(a_idx)
For
angle_th = 0.1
a_list_sort = [2, 3, 4, 4.1, 5, 5.05, 2.1, 3.05, 7, 2.2, 3.2]
it strangely yields
>>> present_a
[2, 3, 4, 5, 2.1, 7, 2.2, 3.2]
while I expect
>>> present_a
[2, 3, 4, 4.1, 5, 2.1, 7, 2.2, 3.2]
because 4.1
is not close enough (with threshold = 0.1
). Note that element 2.1
is somehow processed correctly.
I really have no idea what why is this happening. Am I missing something obvious?