Here are two fonctions which (I thought) should do the same thing but actually do not.
It seems that with the list comprehension, the index taken is the first that could correspond, so when you have the same value at different index, there is an ambiguity.
Is there a way to modify the list comprehension in filter2 so get the same result as in filter1 ?
L = [98.75011926342906,
97.8178200008178,
98.6138182016438,
98.55520874507613,
98.25262038791283,
98.75011926342906,
99.06770073738875,
98.66970163697574,
98.56611283001895,
98.47751713985852,
98.66970163697574,
97.8178200008178]
def filter1(L, threshold=98.7):
items = []
for i in range(len(L)):
if L[i] < threshold:
items.append(i)
return items
def filter2(L, threshold=98.7):
items = [L.index(x) for x in L if x <= threshold]
return items
print filter1(L)
>>> [1, 2, 3, 4, 7, 8, 9, 10, 11]
print filter2(L)
>>> [1, 2, 3, 4, 7, 8, 9, 7, 1]
filter1you use<sign, infilter2you use<=. The result is also different because these two filters use different logic,indexreturns the value of the first index found (that is where the flaw is).