python - The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() with index function
问题描述
Well, in my Python code I'm trying to print out the index of a list of lists using a list, this way:
print(datos.tolist().index(lista_muestra_pequena[1]))
'datos' and 'lista_muestra_pequena' are two lists of lists, and I want to know which index have lista_muestra_pequena[1]
in 'datos'.
'datos' is actually a numpy array, that's why I put .tolist()
One error give me Python when executing, this one:
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()
I understand the function 'a.all()', used for arrays to specify if all elements of the array must satisfy the condition, or only one in 'a.any()' case. However, I don't understand why is telling me this on this case. I tried putting 'np.all()' in two sites on the line, but I don't get it.
解决方案
该.index()
方法不适用于数组作为参数(假设lista_muestra_pequena
是一个数组)。
要获取datos
wherelista_muestra_pequena[1]
的索引,您可以使用numpy.where()方法:
print(np.where(datos == lista_muestra_pequena[1])[0][0]) # first occurrence
推荐阅读
- nlp - 如何获取 word2vec.bin 文件
- swift - 如何在终端中使用 Swift 关闭本地服务器连接?
- c# - C# 将 XPath 与 XmlDocument 一起使用 - 无法选择命名空间中的节点(返回 null)
- java - Java 对象处理
- android - 如何在我的应用程序中禁用 toast 消息?
- c++ - 重定位源文件时读取调试符号
- flutter - 颤动卡片形状
- microsoft-graph-api - Microsoft Graph:使用测试版获取用户日历事件
- php - 带有phpword问题的docx到html
- python - 使用熊猫时无法设置日期时间刻度