Sorting by item_in_another_list

Peter Otten __peter__ at web.de
Tue Oct 24 11:56:22 EDT 2006


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.
> """

I'd say you need not worry about stability since the key parameter to the
list.sort() method was introduced in Python 2.4...

Peter



More information about the Python-list mailing list