For review: PEP 308 - If-then-else expression

Paul Moore gustav at morpheus.demon.co.uk
Sat Feb 8 18:27:03 EST 2003


"Paul F. Dubois" <dubois at users.sourceforge.net> writes:

> Two thumbs way up on 308.
>
> Suppose x is a sequence of floats. Compare:
>
> y = [0.0] * len(x)
> for i in range(len(x)):
>     if x[i] >= 0.0:
>         y[i] = sqrt (x[i])
>
> vs.
>
> y = [(sqrt(z) if z > 0.0 else 0.0) for z in x]

y = [sqrt(max(z, 0.0)) for z in x]

If you think this is a little too obscure, add a comment. I'm assuming
that you'd object to a suggestion that you define a safe_sqrt()
function which returns zero for negative numbers...

Next?
Paul.
-- 
This signature intentionally left blank




More information about the Python-list mailing list