bisect on a list of lists

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Mar 9 16:57:42 EST 2007


En Fri, 09 Mar 2007 17:15:44 -0300, Paulo da Silva  
<psdasilvaX at esotericaX.ptX> escribió:

> 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?
>
> Is there, for lists, something equivalent to the __cmp__ function for
> classes?

lists *are* classes (at least since Python 2.2)
Inherit from the builtin list, redefine __cmp__(self, other) as  
cmp(self[i_th], other[i_th])
and then redefine all the rich comparison methods (__eq__, __lt__, etc)  
using __cmp__, e.g. __le__(self, other) as __cmp__(self, other)<=0 etc.
See http://docs.python.org/ref/customization.html

-- 
Gabriel Genellina




More information about the Python-list mailing list