the PHP ternary operator equivalent on Python
bonono at gmail.com
bonono at gmail.com
Sat Nov 19 04:48:55 EST 2005
Steven D'Aprano wrote:
> L = ["zero" if n == 0 else \
> "negative " + ("odd" if n % 2 else "even") if n < 0 else \
> "odd" if n % 2 else "even" for n in range(8)]
>
BTW, the continuation is not necessary I believe.
[ x==0 and "zero" or ["","-"][x < 0] + ("even", "odd")[x%2] for x in
range(8) ]
isn't too bad. though I like the C one a bit more(as it doesn't
overload the and/or)
[ x==0 ? "zero" : ["","-"][x < 0] + ("even", "odd")[x%2] for x in
range(8) ]
As it is still a very straight forward flow(from left to right, without
"internal decision split in between").
More information about the Python-list
mailing list