I am trying to check three
statements if any of them or two of them is True or False.
But I don't know efficient way to check all three in different ways
at same time.
There are three Booleans
and I am trying to check like :-
1). If boolean_1 is True
and boolean_2 and boolean_3 are False. => do something_1
2). If boolean_2 is True
and boolean_1 and boolean_3 are False. => do something_2
3). If boolean_3 is True
and boolean_1 and boolean_2 are False. => do something_3
4). If boolean_1 and boolean_2 is True
and boolean_3 is False. => do something_4
.
5). If boolean_1 and boolean_3 is True
and boolean_2 is False. => do something_5
.
6). If boolean_2 and boolean_3 is True
and boolean_1 is False. => do something_6
.
7). If boolean_1 is True
and boolean_2 is True
and boolean_3 is True
. => (and guess what) do something_7
.
A way I can do is :-
file.py
if boo_1 == True and bool_2 == False and bool_3 == False:
do_something_1
elif boo_2 == True and bool_1 == False and bool_3 == False:
do_something_2
elif boo_3 == True and bool_2 == False and bool_1 == False:
do_something_3
elif boo_1 == True and bool_2 == True and bool_3 == False:
do_something_4
elif boo_1 == True and bool_3 == True and bool_2 == False:
do_something_5
elif boo_2 == True and bool_3 == True and bool_1 == False:
do_something_6
else:
do_something_7
I can also use this lengthy if elif
method But I don't know why I think that "This is not the efficient way". and I think this will slow. Will this slow ?
Questions
Will this lengthy if elif method be slow ?
Is there a more efficient way instead of this lengthy code ?
Any suggestions would much Appreciated. Thank You
CodePudding user response:
For cleaner code you don't need to check equality vs True/False
as they already evaluate as that. Consider changing:
# from this
if boo_1 == True and bool_2 == False, bool_3 == True:
# to this
if boo_1 and not bool_2 and bool_3:
As for efficiency, consider arranging your if
s so it needs to do the least amount of checks as possible to get it's result. Aside from that consider the below for other options.
Using a dict
to determine what to do in each case:
switch = {
(True, True, True): do_thing_1,
(True, True, False): do_thing_2,
(True, False, False): do_thing_3,
(True, False, True): do_thing_4,
...
}
Then just access using your variables:
switch[(bool_1, bool_2, bool_3)](*args, **kwargs)
Or for an else clause use dict.get
:
switch.get((bool_1, bool_2, bool_3), do_else)(*args, **kwargs)
Using Structural Pattern Matching in python 3.10
match [bool_1, bool_2, bool_3]:
case [True, False, False]:
do_thing_1
case [True, (True | False), False]: # to match a True or False
do_thing_x
case _:
do_else
CodePudding user response:
Well, on such a small scale I doubt this will make any difference, but you could cut it down by if/else in a branched way:
if boo_1:
if boo_2:
if boo_3:
do_something
else:
do_something
else:
if boo_3:
do_something
else:
do_something
else:
if boo_2:
if boo_3:
do_something
else:
do_something
else:
if boo_3:
do_something
else:
do_something
This way you can in best case cut down the number checks down. In your best case, it checks already 3 times, in worst case 18 times. Just to get the conclusion that all have been False. With each if/elif statement it must check for 3 conditions. If you do it in a branched way, it must check in best case 3 conditions, as well as in worst case. The difference is not big, but there is a difference. I quick wrote a program which got to assign to boo_1 to boo_3 a random 0 or 1 and check it through the branched if/else statements and your if/elif/else statement.
After 1mio runs, the branched function had an average duration of 2.8696 sec while your construct had 3.0864 sec.
CodePudding user response:
With numpy
and dict
you can minimize rows
import numpy as np
bool_0 = False
bool_1 = True
bool_2 = True
t = [bool_0,bool_1,bool_2] # this is list of true/False index are [0,1,2]
switch = {0:"--0--",1:"--1--",2:"--2--",'01':"--01--",'12':'--12--','03':'--03--'} # these are conditions to follow
if bool_0 bool_1 bool_2 == 1:
print(switch[np.where(t)[0][0]])
elif bool_0 bool_1 bool_2 == 2:
print(switch[str(np.where(t)[0][0]) str(np.where(t)[0][1])])
else:
print('Either all True or all False')
you need to change conditions inside switch dict
keeping keys
as it is