Logical operations with array of strings in Python
<p>I know the following logical operation works with numpy: </p>

<pre><code>A = np.array([True, False, True])
B = np.array([1.0, 2.0, 3.0])
C = A*B = array([1.0, 0.0, 3.0])
</code></pre>

<p>But the same isn't true if B is an array of strings. Is it possible to do the following:   </p>

<pre><code>A = np.array([True, False, True])
B = np.array(['eggs', 'milk', 'cheese'])
C = A*B = array(['eggs', '', 'cheese'])
</code></pre>

<p>That is a string multiplied with False should equal an empty string. Can this be done without a loop in Python (doesn't have to use numpy)?</p>

<p>Thanks!</p>

<p>You can use <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html"><code>np.where</code></a> for making such selection based on a mask -</p>

<pre><code>np.where(A,B,'')
</code></pre>

<p>Sample run -</p>

<pre><code>In [4]: A
Out[4]: array([ True, False,  True], dtype=bool)

In [5]: B
Out[5]: 
array(['eggs', 'milk', 'cheese'], 
      dtype='|S6')

In [6]: np.where(A,B,'')
Out[6]: 
array(['eggs', '', 'cheese'], 
      dtype='|S6')
</code></pre>

python string numpy logical-operators