Algorithm help per favore

Anton Vredegoor anton at vredegoor.doge.nl
Thu Jun 19 08:23:02 CEST 2003


Alex Martelli <aleax at aleax.it> wrote:

>> [x for x, y in zip(L, [lambda x:x]+L) if x != y]
>> 
>> There must be a better way to do this than with a lambda.

<snip lambda>

>What about L[:1] + [x for x,y in zip(L[1:],L[:-1]) if x!=y] as
>a less-tricky variant of your list-comprehension?  Does need L
>to be a non-empty list (not another sequence, not an empty one),
>but that might be acceptable perhaps...

Nice idea, there seems to be no problem with L an empty list, but
there would be a problem with L a tuple. However that can be
corrected:

   [x for x,y in zip(L,L[1:]+L[:-1]) if x!=y] or list(L[:1])

Anton








More information about the Python-list mailing list