Sorting by item_in_another_list

Carsten Haese carsten at uniqsys.com
Tue Oct 24 17:41:37 CEST 2006


On Tue, 2006-10-24 at 11:31, Gabriel Genellina wrote:
> At Tuesday 24/10/2006 04:35, Cameron Walsh wrote:
> 
> > > c = set(B)
> > > a.sort(key=c.__contains__, reverse=True)
> > >
> > > Tim Delaney
> >
> >Depressingly simple.  I like it.
> 
> ...and fast!

...and not guaranteed to be correct:

http://www.python.org/doc/2.3.5/lib/typesseq-mutable.html states:

"""
Whether the sort() method is stable is not defined by the language (a
sort is stable if it guarantees not to change the relative order of
elements that compare equal). In the C implementation of Python, sorts
were stable only by accident through Python 2.2. The C implementation of
Python 2.3 introduced a stable sort() method, but code that intends to
be portable across implementations and versions must not rely on
stability.
"""

-Carsten





More information about the Python-list mailing list