Algorithm help per favore

David Eppstein eppstein at ics.uci.edu
Wed Jun 18 16:27:46 EDT 2003


In article <3v3Ia.109349$pR3.2389728 at news1.tin.it>,
 Alex Martelli <aleax at aleax.it> wrote:

> > 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.
> 
> Actually, "lambda:0" is a good candidate for "an object that's
> not gonna equal any object with a different id()" -- it's just
> 8 characters.  "object()" is also 8 characters.  Not sure what
> you mean by "quickest" or "better", but number of characters in
> the source seems an objective-enough measurement.

Sorry for the imprecise language.  I think what I really meant was 'most 
pythonic'.  I prefer object() to lambda:0 since lambda to me implies 
some kind of functional programming which is not present here.

It's tempting to use "not L[0]" instead (since what I want is an object 
that's not equal to L[0]) but that fails when L is empty.

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list