jeudi 26 novembre 2020

How do I tell if exactly N conditions in a set of M conditions are true in Python?

The title is a bit confusing, so I'll give an example. Say I have a set of 4 conditions, [A,B,C,D], where each condition can either be true or false. For example, condition A can either say

A[i,j+1] == 0 

or

A[i,j+1] != 0

I want an if statement that essentially says the following:

if (in [A,B,C,D], any 2 are true and any 2 are false):
    do something
else:
    do something else

I could hard-code each individual case if necessary. For example:

if ((A and B are true) and (C and D are false)) or  ((A and C are true) and (B and D are false)) or...

and I can just continue that for every possible case. However, that seems wildly inefficient and not pythonic.

What's the best method to do this? Should I just hard-code it? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire