Algorithm help per favore

Bengt Richter bokr at oz.net
Wed Jun 18 16:03:03 EDT 2003


On Wed, 18 Jun 2003 11:56:27 -0700, David Eppstein <eppstein at ics.uci.edu> wrote:

>In article <3EF0AAFB.DDA5F555 at engcorp.com>,
> Peter Hansen <peter at engcorp.com> wrote:
>
>> > >>> L = [6,3,3,3,5,7,6,3,4,4,3]
>> > >>> [x for x, y in zip(L, [L]+L) if x != y]
>> > [6, 3, 5, 7, 6, 3, 4, 3]
>> 
>> But this won't work if L contains a reference to L!  :-) :-)
>
>Ok, what's the quickest way to build a new object that's guaranteed 
>unequal to any previous object?  E.g.
>[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.
>
 >>> L = [6,3,3,3,5,7,6,3,4,4,3]
 >>> [x for t in [[]] for x in L if (not t or x!=t.pop(), t.append(x))[0]]
 [6, 3, 5, 7, 6, 3, 4, 3]

Not very tested ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list