擅长:python、mysql、java
<p>定义一个函数,该函数接受要搜索的项和要搜索的数组的列表,并使用循环在每个数组中查找该项。使用异常处理来捕获<code>IndexError</code>。在</p>
<pre><code>>>> import numpy as np
>>> v = np.array([[1, 2, 3]])
>>> r = np.array([[1, 2, 3], [0, 9, 8], [2, 4, 4]])
>>> l = np.array([[4, 5, 6], [7, 8, 9]])
def get_index(seq, *arrays):
for array in arrays:
try:
return np.where(array==seq)[0][0]
except IndexError:
pass
...
>>> get_index(v, r, l)
0
>>> get_index(np.array([7, 8, 9]), r, l)
1
</code></pre>
<p>如果在任何数组中都找不到该项,那么您将得到<code>None</code>作为输出。在</p>
<p><strong>更新:</strong></p>
<p>如果还需要名称,请在字典中传递数组:</p>
^{pr2}$