conditional expression sought

Dave K dk123456789 at REMOVEhotmail.com
Fri Jan 30 01:47:25 CET 2004


On Thu, 29 Jan 2004 17:58:45 GMT in comp.lang.python, "Elaine Jackson"
<elainejackson7355 at home.com> wrote:

>If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True,
>then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j without
>evaluating any other B_i. This is such a useful mode of expression that I would
>like to be able to use something similar even when there is an i with
>bool(B_i)==False. The only thing I can think of by myself is ( (A_1 and [B_1])
>or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for obvious
>reasons. Does anybody know a good way to express this? Any help will be mucho
>appreciado.
>
>Peace
>

I'm not sure if this is what you're looking for, but for i > a very
small number, the zip function seems more appropriate:

>>> def get_B_i(A, B):
	    for a, b in zip(A, B):
		    if a: return b
	    return A[-1]
>>> print get_B_i([False, False, True, False], [0, 1, 2, 3])
2
>>> print get_B_i([False, False, False, False], [0, 1, 2, 3])
False

This has exactly the same effect provided that A and B are the same
length, otherwise the return value by failure should be adjusted to
whatever suits your purpose.

If you really want to write a conditional expression out in full, I
can't think of anything non-clumsy that would work for all possible
values of B_i, so unfortunately can't help you there.

Dave





More information about the Python-list mailing list