包含多个元素的数组的真值不明确。将.any()或.all()与索引函数一起使用

2024-04-20 08:02:58 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,在我的Python代码中,我试图用一个列表打印出一个列表列表的索引,如下所示:

print(datos.tolist().index(lista_muestra_pequena[1]))

“datos”和“lista\u muestra\u pequena”是两个列表列表,我想知道哪个索引在“datos”中有lista_muestra_pequena[1]。 “datos”实际上是一个numpy数组,这就是为什么我把.tolist()

执行Python时出现一个错误,这个错误: print(datos.tolist().index(lista_muestra_pequena[1]))

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我理解函数“a.all()”,用于数组,指定数组的所有元素是否必须满足条件,或者在“a.any()”情况下仅满足一个条件。不过,我不明白你为什么要告诉我这个案子。我试着把np.全部()“在两个网站上线,但我不明白。你知道吗


Tags: 代码numpy列表index错误any数组all
1条回答
网友
1楼 · 发布于 2024-04-20 08:02:58

.index()方法将不使用数组作为参数(假设lista_muestra_pequena是数组)。你知道吗

要获取datos的索引(其中lista_muestra_pequena[1]),可以使用numpy.where()方法:

print(np.where(datos == lista_muestra_pequena[1])[0][0]) # first occurrence

相关问题 更多 >