Whither binary search?
Fredrik Lundh
fredrik at pythonware.com
Tue Sep 26 15:44:58 EDT 2006
Neil Cerutti wrote:
>> bisect...
>
> That doesn't tell me if an item doesn't exist in the sequence
> though, does it? Maybe I'm being dense.
I guess you could use something like
import bisect
def check(list, item):
try:
return list[bisect.bisect_left(list, item)] == item
except IndexError:
return False
but unless your lists are really large, or your objects are expensive to
compare, you could probably just use "in" instead.
> My guess nobody keeps their sequences sorted in Python.
well, people tend to use dictionaries when they need to look things up
quickly...
</F>
More information about the Python-list
mailing list