Question on lists

Martin Maney maney at pobox.com
Mon Aug 9 18:40:31 EDT 2004


Aahz <aahz at pythoncraft.com> wrote:
> Elaine Jackson <elainejackson7355 at home.com> wrote:
>>
>>If x is the given sequence, then
>>[x[i] for i in filter(lambda i: i==0 or x[i-1]<>x[i], range(len(x)))]
>>is what you want.

> Please report to the UnPythonic Activities Committee.  ;-)

Has the committee considered the other functional solution?

>>> def dedupe(lst, x):   
...     if not lst or lst[-1] != x:
...             lst.append(x)
...     return lst
... 
>>> reduce(dedupe, l, [])
['a', 'b', 'c', 'd', 'e']

If it would help, I'm sure we could get an obfuscation expert to make
it into one line, but without a first-class conditional expression,
it's nothing but a circus trick.  Perl envy, like.

Oh, okay, twist my arm...

>>> reduce(lambda r,x: (not r or r[-1]!=x) and r.append(x) or r, l, [])
['a', 'b', 'c', 'd', 'e']

I knew reduce would come in handy for something.

> "To me vi is Zen.  To use vi is to practice zen.  Every command is a
> koan.  Profound to the user, unintelligible to the uninitiated.  You
> discover truth everytime you use it."  --reddy at lion.austin.ibm.com

Apparently vi also shares with koans the characteristic of battering
down the walls of rationality.  :-)

-- 
Although we may never know with complete certainty the identity
of the winner of this year's presidential election, the identity
of the loser is perfectly clear.  It is the nation's confidence
in the judge as an impartial guardian of the law.
 - Justice John Paul Stevens, from his dissenting opinion Dec 12, 2000



More information about the Python-list mailing list