<pre><code>import numpy as np
vec = np.array([2, 2, 2, 51, 51, 52, 52, 14, 14, 14, 51, 51, 52, 52])
first_occurrence = []
for x in np.unique(vec):
if x not in [51,52]:
first_occurrence.append(np.argmax(x==vec))
</code></pre>
<p><a href="https://numpy.org/doc/stable/reference/generated/numpy.argmax.html" rel="nofollow noreferrer">^{<cd1>}</a>查找布尔数组<code>x==vec</code>中第一次出现的最大值(即<code>True</code>)的索引。由于<code>x</code>来自<code>vec</code>,因此保证至少有一个<code>True</code>值</p>
<hr/>
<p>性能取决于<code>vec</code>的大小和要查找的值的数量。对于较大的数组,这种简单循环方法(蓝色)优于<a href="https://stackoverflow.com/a/66102553/3944322">accepted answer</a>(绿色和橙色),特别是对于在示例中找到的少量值(对于给定的玩具示例,它实际上快了1.7倍)(<a href="https://gist.github.com/StefRe/1d73c663d5b162cbda7bb3dd6d838a19" rel="nofollow noreferrer">source</a>)。
<a href="https://i.stack.imgur.com/28dde.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/28dde.png" alt="enter image description here"/></a>
<a href="https://i.stack.imgur.com/a8ITd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/a8ITd.png" alt="enter image description here"/></a>
事实证明,将<code>unique</code>与<code>index=True</code>一起使用相对较慢,较大数组的另一个因素是掩码的内存分配</p>