擅长:python、mysql、java
<h3>纽比0.13+</h3>
<p>从NumPy v0.13开始,您可以使用<a href="https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.isin.html" rel="nofollow noreferrer">^{<cd1>}</a>,它可以在多维数组上工作:</p>
<pre><code>>>> element = 2*np.arange(4).reshape((2, 2))
>>> element
array([[0, 2],
[4, 6]])
>>> test_elements = [1, 2, 4, 8]
>>> mask = np.isin(element, test_elements)
>>> mask
array([[ False, True],
[ True, False]])
</code></pre>
<h3>NumPy pre-0.13</h3>
<p>使用<code>np.in1d</code>接受的答案仅适用于1d数组,需要重新调整形状以获得所需的结果。这对v0.13之前的NumPy版本很好。</p>