擅长:python、mysql、java
<p>根据你的描述,我做了几个假设:<br/>
-2d数组是矩形的(即不是<code>dtype=object</code>),否则NumPy性能将毫无用处。<br/>
-<code>len(fully_split) == len(gene_name_list)</code>因为您的代码示例有<code>ensembl_list[idx] = row[0]</code>,<code>idx</code>是从<code>gene_name_list</code>派生的</p>
<pre><code>>>> gene_name_list = np.array('a bb c d eee'.split())
>>> fully_split = np.array([
... 'id1 a bb c d eee'.split(), # yes
... 'id2 f g hh iii j'.split(),
... 'id3 kk ll a nn o'.split(), # yes
... 'id4 q rr c t eee'.split(), # yes
... 'id5 v www xx y z'.split()
... ])
>>> longest_gene_name = len(max(gene_name_list, key=len))
>>> dtype = 'U{}'.format(longest_gene_name)
>>> ensembl_list = np.zeros_like(gene_name_list, dtype=dtype)
>>> mask = np.isin(fully_split, gene_name_list).any(axis=1)
>>> ensembl_list[mask] = fully_split[mask, 0]
>>> ensembl_list
array(['id1', '', 'id3', 'id4', ''], dtype='<U3')
</code></pre>