Home > Mobile >  Checking probability of the if statement in Python more efficiently
Checking probability of the if statement in Python more efficiently

Time:11-10

I have multiple variables to pass in if, elif and else statement. Assuming 3 variables a, b, and c. Those are simply list that contains numbers. But I need to define if, elif and else statement for each probability of the variables.

For example:

  • if one of the variables >0 do something with this variable but pass the others.

Based on probability I know the all the possibilities and therefore I prepare the code based on these possibilities

weeks =9

a=[1,0,1,1,1,0,0,0,1]
b=[1,0,0,1,0,1,1,0,1]
c=[1,0,0,0,1,0,1,1,1]
for i in range (weeks):
    if i <= 0:
        (print('this is hypo'))
        
    else:    
        if(a[i] <= 0 and b[i] <= 0 and c[i] <= 0):  # no prod         0
            print(a[i],b[i],c[i],'no ne is working')
        elif(a[i] > 0 and b[i] <= 0 and c[i] <= 0): # only first      1
            print(a[i],b[i],c[i],'only a working')
        elif(a[i] > 0 and b[i] > 0 and c[i] <= 0): #first and second  1-2
            print(a[i],b[i],c[i],'a and b working')
        elif(a[i] > 0 and b[i] <= 0 and c[i] > 0): # first and third  1-3
            print(a[i],b[i],c[i], 'a and c working')
        elif(a[i] <= 0 and b[i] > 0 and c[i] <= 0): # only second     2
            print(a[i],b[i],c[i],'only b working')
        elif(a[i]<= 0 and b[i] > 0 and c[i] > 0): #second and third   2-3
            print(a[i],b[i],c[i],'b and c working')
        elif(a[i] <= 0 and b[i] <= 0 and c[i] > 0):     # only third  3
            print(a[i],b[i],c[i],'only c working')
        else:                            # all of are working         1-2-3
            print (a[i],b[i],c[i], 'all wokring')
        print('iteration number :',i)

What I'm trying to achieve is finding an efficient way to pass these possibilities in few statements. It is not a big issue to deal with 3 variables, but what happens if I want to pass 10 variables. Do I need to define each probability separately ?

CodePudding user response:

You could tally up all that are working and if none or all are working then short circuit in a sense, and if only some are then print the ones that are:

a=[1,0,1,1,1,0,0,0,1]
b=[1,0,0,1,0,1,1,0,1]
c=[1,0,0,0,1,0,1,1,1]

for abc in zip(a, b, c):
    print(*abc, end=" ")
    result = [x * z for x, y in zip("abc", abc) if (z := y > 0)]
    if not result:
        print("None working")
    elif len(result) == len(abc):
        print("All working")
    else:
        print("Only", " and ".join(result), "working")

Result:

1 1 1 All working
0 0 0 None working
1 0 0 Only a working
1 1 0 Only a and b working
1 0 1 Only a and c working
0 1 0 Only b working
0 1 1 Only b and c working
0 0 1 Only c working
1 1 1 All working

CodePudding user response:

More simplar way?

a=[1,0,1,1,1,0,0,0,1]
b=[1,0,0,1,0,1,1,0,1]
c=[1,0,0,0,1,0,1,1,1]

a1 = ['working' if int(el)>0 else 'not working' for el in a]
b1 = ['working' if int(el)>0 else 'not working' for el in b]
c1= ['working' if int(el)>0 else 'not working' for el in c]

for f, b,i,x,y,z in zip(a, b,c,a1,b1,c1):
    print(f, b,i,x,y,z)

output #

1 1 1 working working working
0 0 0 not working not working not working
1 0 0 working not working not working
1 1 0 working working not working
1 0 1 working not working working
0 1 0 not working working not working
0 1 1 not working working working
0 0 1 not working not working working
1 1 1 working working working
  • Related