Sorting by item_in_another_list

Carsten Haese carsten at uniqsys.com
Tue Oct 24 11:50:17 EDT 2006


On Tue, 2006-10-24 at 11:41, Carsten Haese wrote:
> 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

And I noticed a bit to late that the search on python.org lead me to an
outdated version of the docs.

The current documentation states that "Starting with Python 2.3, the
sort() method is guaranteed to be stable." However, it's not clear
whether this specifies language behavior that all implementations must
adhere to, or whether it simply documents an implementation detail of
CPython.

-Carsten





More information about the Python-list mailing list