-4

i am using numpy :

In [83]: names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])

In [93]: mask = (names == 'Bob') | (names == 'Will')

why can not I use "or" instead of "|"?

why does it give me error when I remove the pretenses in In[93] ?

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Thanks in advance.

someone
  • 205
  • 3
  • 14

1 Answers1

2

and is python's logical-AND operator.

| is python's bitwise-OR operator (not bitwise-AND, as your question might suggest), which numpy overrides in order to make it the numpy element-wise-OR.

Numpy couldn't have overridden and nor or to work element-wise, because and and or are not overridable in python. Therefor, the bitwise operators are overridden in numpy.

pippin1289
  • 4,861
  • 2
  • 22
  • 37
shx2
  • 61,779
  • 13
  • 130
  • 153