Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If X or Y or Z then use *that* one?

Is there a way to write an If (or equivalent) statement that can have many arguments, and if any of those satisfy the logic, use that variable?

For instance

if len(x) == 1 or len(y) == 1 or len(z) == 1 or ... len(zz) == 1:
    # do something with the variable that met the condition

So say only z has length 1, could I write above idea/formula in a way that takes the first True answer and use that?

So something like

x = "123"
y = "234"
z = "2"
xx = "1234"
yy = "12345"

if len(x) == 1 or len(y) == 1 or len(z) == 1 or len(xx) == 1 or len(yy) == 1:
    #do something with the variable that satisfies the condition, so `z` in this case.

Does that make any sense? The variables' lengths could change any time, so I'd like to be able to say "If any of the conditions are met, use the variable that met the condition"...?

In the above, I don't know beforehand that zwill be the only one to meet the criteria, so my Then statement can't be z = "new value" or whatever I want to do with it.

Edit: Sorry, per comments I know checking for len on integers isn't okay. This is solely for illustration purposes and it was the first thing I thought of to "test". Sorry if the len bit is confusing. I'm mainly just trying to see if I can use If statements (or related ones) where I don't know which of my many variables will meet a condition. (I'm still new regarding python, so my sincere apologies for my lack of semantics or proper terms). I'd like to avoid elif if at all possible just because it can get stringy. (But if that's the most pythonic way, then so be it!)

like image 425
BruceWayne Avatar asked Aug 10 '18 19:08

BruceWayne


People also ask

How do you do X or Y in Excel?

The Excel OR function returns TRUE if any given argument evaluates to TRUE, and returns FALSE if all supplied arguments evaluate to FALSE. For example, to test A1 for either "x" or "y", use =OR(A1="x",A1="y").

Can you combine if and/or functions in Excel?

IF Function is one of the most powerful functions in excel. And, the best part is, you can combine other functions with IF to increase its power. Combining IF and OR functions is one of the most useful formula combinations in excel.

Can you have multiple IF THEN statements in Excel?

While Excel will allow you to nest up to 64 different IF functions, it's not at all advisable to do so.


3 Answers

While @pault 's answer addresses your question, I think it isn't super readable. If you have a couple of variables only, pythons mantra dictate a straightforward, explicit way:

if len(x) == 1:
  f(x)
elif len(y) == 1:
  f(y)
elif len(z) == 1:
  f(z)

Otherwise, if you have a list, a for loop is readable and efficient:

for l in ls:
    if len(l) == 1:
        f(l)
        break
like image 108
erenon Avatar answered Oct 24 '22 09:10

erenon


You could use next here to pick the first item out of a list of options that meets your criteria:

value = next((item for item in [x, y, z] if len(item)==1), None)
if value is not None:
    ...

The second argument to next() is the default value if no values meet your criteria.

like image 10
pault Avatar answered Oct 24 '22 10:10

pault


What you describe has a general implementation called first_true in the itertools recipes.

def first_true(iterable, default=False, pred=None):
    """Returns the first true value in the iterable.

    If no true value is found, returns *default*

    If *pred* is not None, returns the first item
    for which pred(item) is true.

    """
    # first_true([a,b,c], x) --> a or b or c or x
    # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
    return next(filter(pred, iterable), default)

Example

value = first_true([x, y, z], pred=lambda x: len(x) == 1)

if value:
    ...
like image 4
Olivier Melançon Avatar answered Oct 24 '22 09:10

Olivier Melançon