bisect on a list of lists

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Mar 9 16:59:16 EST 2007


Paulo da Silva:
> What is the best way to have something like the bisect_left
> method on a list of lists being the comparision based on an
> specified arbitrary i_th element of each list element?

You may use this recipe of mine that I've just put there for you, but
it's not fully tested yet (if you spot problems I'm willing to fix
them):
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/502295

Or you can create a little class, that wraps your list with just a
__cmp__ method that works on the n-th element of the list. Then you
can use heapq on many instances of that class created with a list
comphrension.

Probably there are other solutions too.

Bye,
bearophile




More information about the Python-list mailing list