擅长:python、mysql、java
<p>使用<code>np.where</code>获取值小于0.5的索引,然后打印这些元素:</p>
<pre><code>for i in range(5):
list1 = ['A', 'B', 'C', 'D', 'E']
mask = np.where(np.random.uniform(low= 0.0, high= 1.0, size=5) < 0.5)
print([list1[i] for i in mask[0]])
#output (The output is stochastic meaning they will change on each iteration unless you use fixed random state)
['C']
['A', 'B', 'C', 'E']
['D', 'E']
['A', 'C', 'D']
['B', 'C', 'E']
</code></pre>