DSU pattern (was Re: Trouble sorting lists (unicode/locale related?))

Alex Martelli aleax at aleax.it
Mon Sep 22 15:35:31 CEST 2003


Peter Otten wrote:

> Jeff Epler wrote:
> 
>> If this did work, then you can use the DSU pattern like so:
>>
>> decorated_wordlist = [(locale.strxfrm(w), w) for w in wordlist]
>> decorated_wordlist.sort()
>> wordlist[:] = [i[1] for i in decorated_wordlist]
> 
> Everytime that someone posts a naive list.sort(compare), the DSU pattern
> is proposed to improve execution speed.
> 
> So maybe it's about time to change the sort() method to support a second
> argument
> 
> list.sort(compare=None, mapping=None)
> 
> that, if provided, would perform the DSU magic. Or was that already
> proposed and rejected?

I have not seen this proposed before, and I'm not very clear on what
the "compare" and "mapping" arguments are supposed to be in order to
let you specify any DSU.  Basically it seems you would need two
callables, "decorate" to be called with each item in the list (to
return for each item the decorated tuple) and "undecorate" to be
called with each decorated tuple after the sort (to return the item
for the result).  How do you turn that into "compare" and "mapping"?


Alex





More information about the Python-list mailing list