python - Seeking a fast filter() with removal -
i'm trying write reasonably fast quicksort, has usage in many other applications.
the built in filter(function, iterable) function returns list of items in iterable when passed function return true, , faster traditional loop when need check 1 condition 1 list.
what looking function fast (like filter) not construct new list, but remove items takes old list. would, in application of single-pivot quicksort, allow removal of filter statement , near 2x speedup of partition routine.
is there such function built in python? numpy? what's fastest way implement if not?
for reference, here current partition code:
def partition(u): lesser = singlequicksort(filter(lambda num: num <= u[0], u[1:])) greater = singlequicksort(filter(lambda num: num > u[0], u[1:])) return lesser, greater
using boolean mask:
def partition(u): mask = u[1:] <= u[0] return u[1:][mask], u[1:][~mask]
Comments
Post a Comment