Misuse of list comprehensions?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Tue May 27 12:30:35 EDT 2008


Simon Forman a écrit :
> On May 21, 4:36 am, Bruno Desthuilliers <bruno.
> 42.desthuilli... at websiteburo.invalid> wrote:
>> Simon Forman a écrit :
(snip)
>>> If order is important, you can use sorted() instead of list() like so:
>>> def compress(s):
>>>     new = sorted(set(s), key=s.index)
>>>     return return ''.join(new)
>> This won't still preserve the *original* order.
> 
> 
> I don't understand.
> 
> new will contain each unique item in s, sorted in order of the items'
> first occurance in s, right?

Yep. Answered too fast, missed the key param. Sorry.




More information about the Python-list mailing list