y=if x>High: 2 elif x<Low: 1 else: 0
Steven Majewski
sdm7g at Virginia.EDU
Wed May 22 19:12:10 EDT 2002
On Wed, 22 May 2002, Hans Nowak wrote:
> Andrew Dalke wrote:
>
> > Hans Nowak wrote:
> > >It may be possible to put everything on one line using
> > >some obfuscated functional idiom, but I won't go there. :-)
> >
> > No problem, I will
> >
> > y = (x>high)*2 + (x<low)
> >
> > This assumes low <= high.
> >
> > But please don't use this in real code.
>
> Ah, well, this seems kinda alright... I was thinking along the
> lines of the infamous
>
> (a and [b] or [c])[0]
>
> idiom... I don't know if a variant of this is possible in this
> case, and probably don't want to know. :-)
>
I prefer:
>>> ( 2, 1, 0 )[ [ x > hi, x < low, 1 ].index(1)]
as more readable, although the lambda functional version gets a bit
more baroque:
>>> (lambda x: (2,1,0)[(lambda x: [x > hi, x < low, 1])(x).index(1)])(arg)
-- Steve Majewski
More information about the Python-list
mailing list