jeudi 18 juin 2020

Multiple conditions if statement syntax in python

I have a problem that I know the workaround to, but I don't understand why my initial attempt does not work. It's a relatively simple Q on multiple conditions for if-statements.

a = np.arange(2)
b = np.arange(5)
c = np.arange(6)

arr = np.array([[i,j,k] for i in a for j in b for k in c])

for row in arr:
    i,j,k = row
    if i != 1 | j != 4 | k != 1: continue
    print(i,j,k)

My expectation is that as long as one of the conditions in the if statement evaluates true, then the for loop should continue to the next line.

The output is that it prints

0 4 1
0 4 5
1 0 0
1 0 1
1 0 2
1 0 3
1 0 4
1 0 5
1 1 0
1 1 1
1 1 2
1 1 3
1 1 4
1 1 5
1 4 1
1 4 5

For example, the very first output, 0 != 1 clearly should evaluate true, therefore it should not print? In fact, the only print I expect to see should be (1,4,1).

I know the solution is

if (i,j,k) != (1,4,1): continue

However, I am puzzled to why my initial attempt does not work as expected..

Cheers

Aucun commentaire:

Enregistrer un commentaire