0

In my real case I have an array of shape (8,2) and I need to check if two integers are of different parity. For example here I want that the line 0 return False (8 and 2 are even, so same parity) line 1 return True (10 is an even number and 3 an odd number) and so on.

[[ 8  2]
[10  3]
[12 1]
[5 6]] 

I would like the result to be in an array like that :

array([ False, True, True, True], dtype=bool)

I thought to use the np.all function but I dont know how to do it.

1 Answers1

1

You could sum them and verify if the sum is even:

import numpy as np

a = np.array([[8, 2],
              [10, 3],
              [12, 1],
              [5, 6]])

result = (a.sum(1) % 2).astype(bool)
print(result)

Output

[False  True  True  True]

If both have the same parity the sum is even, otherwise is odd.

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76