Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

y=if x>High: 2 elif x<Low: 1 else: 0

0 views
Skip to first unread message

gr

unread,
May 22, 2002, 5:39:27 PM5/22/02
to
Hi,
is there a simple possiblity to "teach" Python:

y=if x>High: 2 elif x<Low: 1 else: 0

instead of

if x>High:
y=2
elif x<Low:
y=1
else:
y=0

Thanks,
gr

Hans Nowak

unread,
May 22, 2002, 5:59:57 PM5/22/02
to


You mean, putting everything on one line? No. The best
you can get here is

if x > high: y = 2
elif x < low: y = 1
else: y = 0

It may be possible to put everything on one line using
some obfuscated functional idiom, but I won't go there. :-)

--
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/

holger krekel

unread,
May 22, 2002, 5:48:20 PM5/22/02
to
gr wrote:
> Hi,
> is there a simple possiblity to "teach" Python:
>
> y=if x>High: 2 elif x<Low: 1 else: 0

don't let anyone see this :-)

> instead of
>
> if x>High:
> y=2
> elif x<Low:
> y=1
> else:
> y=0

this works...

y = x<Low and 1 or x>High and 2 or 0

but don't show it around too much, either :-)
the if-elif-else is usually understood best.

holger


Pearu Peterson

unread,
May 22, 2002, 5:56:49 PM5/22/02
to

On 22 May 2002, gr wrote:

> Hi,
> is there a simple possiblity to "teach" Python:
>
> y=if x>High: 2 elif x<Low: 1 else: 0

Try

y = (x>High)*2 or (x<Low)

Pearu

Andrew Dalke

unread,
May 22, 2002, 6:19:55 PM5/22/02
to
Hans Nowak wrote:
>You mean, putting everything on one line? No. The best
>you can get here is
>
> if x > high: y = 2
> elif x < low: y = 1
> else: y = 0
>
>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.

Andrew
da...@dalkescientific.com

Hans Nowak

unread,
May 22, 2002, 6:30:43 PM5/22/02
to
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. :-)

Steven Majewski

unread,
May 22, 2002, 7:12:10 PM5/22/02
to

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

Stephen Horne

unread,
Jun 4, 2002, 6:52:41 PM6/4/02
to
On Wed, 22 May 2002 19:12:10 -0400, Steven Majewski
<sd...@Virginia.EDU> wrote:

>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)

Hmmm

I occasionally use the following little trick...


def IF(p_Condition, p_True, p_False=lambda: None) :
"""
Expects parameterless functions or lambdas for the p_True
and p_False arguments
"""
if p_Condition : return p_True ()
return p_False ()

#...

print IF(x != 0, lambda: 5 // x)


This could easily be adapted...

print IF(x>High, lambda:2, IF(x<Low, lambda:1, lambda:0))


and if multi-condition patterns are extensively used...

def FIRST (p_Tests, p_Default) :
"""
p_Tests : a sequence of 2-tuples, each containing
0 : A parameterless function for the test
1 : A parameterless function for the value
p_Default : A parameterless function for the default
result if all tests fail.

Example...

FIRST( [(lambda: x<Low, lambda: 1),
(lambda: x>High, lambda: 2) ], lambda: 0 )

"""
for l_Test,l_Value in p_Tests :
if l_Test () :
return l_Value ()
return p_Default ()

if p_Lo_Test () : return p_Lo_Result ()
if p_Hi_Test () : return p_Hi_Result ()
return p_Mid_Result ()


Actually, I prefer to use 'None' as the fail case in this pattern so
that the test and result can be bound in one function.

def FIRST (p_Functions) :
"""
p_Functions : a sequence of parameterless functions,
each either returning a valid result or
None.

Example...

FIRST( [(lambda: x<Low and 1 or None),
(lambda: x>High and 2 or None),
(lambda: 0 ) ] )

"""
for l_Function in p_Functions :
l_Result = l_Function ()
if l_Result != None : return l_Result
return None


Personally, I think there should be an if 'function' built in which
has similar semantics to the IF function above but without needing the
lambdas. It would be nice if it also supported more than three
parameters - each pair giving an 'if' or 'elif' condition plus result
and the final odd parameter giving the 'else' result.

This would allow something like...

if (x < Low : 1, x > High : 2, 0)

0 new messages