Thursday, February 25, 2010

numpy.searchsorted

i don't know why i keep forgetting how to find the first or last index of a value inside a numpy array. quick googling will turn up things like (findIn == toFind).all(1).nonzero()[0][0], which works but gets _really_ slow for large arrays since it's searching the whole thing. searchsorted is all i need, 99% of the time, and i don't know why it's not cited more often. i finally remembered (again) this latest time when i found myself reinventing a binary search (which is what searchsorted does).

No comments: