擅长:python、mysql、java
<p>ibredeson的答案是你的具体情况。如果您有一个数组a,并且希望构造一个形状相同的数组b,该数组b只接受两个值,这取决于a上的条件,请考虑使用<code>np.where</code>(请参见<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html" rel="nofollow">the doc here</a>):</p>
<pre><code>import numpy as np
a = np.array([0, 1, 0.3, 0.5])
b = np.where(a > 0.5, 2, 7) # 2 and 7 are the values you want to see in b, whether the
# corresponding element in a is lesser than 0.5 or not.
>>> b
array([7, 2, 7, 7])
</code></pre>