Oct 19, 2023
But the function itself is truly horrible.
That last loop, which repeatedly deletes elements from the start of an array, is quadratic in the length of vec
because each deletion potentially moves every single item in the list.
Those last five lines could just be:
return [v for v, n in zip(vec, normed) if -2 <= n <= 2]
which is shorter, IMHO clearer, and linear in the length of vec
, or even perhaps
yield from (v for v, n in zip(vec, normed) if -2 <= n <= 2)
because why compute the whole list if you don’t need to?