FairyOnIce
FairyOnIce

Reputation: 2614

Python: operation on a vector of True/False

I have been using R for long time and now I am learning Python.

In R when there are vectors of TRUE/FALSE, say vec1 and vec2, I can easily operate on them as:

 vec1 <- c(TRUE,TRUE,FALSE)
 vec2 <- c(FALSE,TRUE,TRUE)


 (vec1ORvec2 <- vec1 | vec2)
 [1] TRUE TRUE TRUE

 (vec1Andvec2 <- vec1 & vec2)
 [1] FALSE  TRUE FALSE

In Python, given vec1 and vec2 is there way to obtain vec1ORvec2 and vec1ANDvec2 without writing a loop?

Upvotes: 2

Views: 1001

Answers (1)

Akavall
Akavall

Reputation: 86178

I think you are looking for numpy.array.

In [4]: import numpy as np

In [5]: a = np.array([True, True, False])

In [6]: b = np.array([False, True, True])

In [7]: a | b
Out[7]: array([ True,  True,  True], dtype=bool)

In [8]: a & b
Out[8]: array([False,  True, False], dtype=bool)

Upvotes: 4

Related Questions