[Tutor] List comprehensions to search a list--amazing!
boB Stepp
robertvstepp at gmail.com
Tue Mar 24 02:42:23 CET 2015
On Thu, Mar 19, 2015 at 12:10 AM, Dave Angel <davea at davea.name> wrote:
> The catch to a list comprehension is it has to visit all the elements, while
> a binary search would visit log-base-2 of them. So instead of 10000
> elements, you'd be searching about 14 items.
I suspected as much, but had not verified this. Nonetheless, this may
prove sufficiently fast. I will have to test this with my final data
files. Right now I am using test cases, while I continue to design,
check, rewrite, etc.
> For large lists, it'd probably be much quicker to use the bisect module.
> https://docs.python.org/3.4/library/bisect.html
Can you give me a ballpark number for "large", where this would start
making a meaningful difference?
> Check out bisect.bisect_left() and bisect.bisect_right()
It looks like this should work. Thanks! I will investigate.
> I don't see how to directly use those functions on a list which is
> reverse-sorted, but the source is available. On my install, it's located
> at:
>
> /usr/lib/python3.4/bisect.py
And I see this is available on my oldest Python installlation, 2.4.4, too.
--
boB
More information about the Tutor
mailing list