Python: List Of Numpy Arrays, Can't Do Index()?
centers is a list, [ ], of numpy arrays. shortest_dist[1] is an numpy array. However, when I do: centers.index(shortest_dist[1]) It tells me ValueError: The truth value of an ar
Solution 1:
When you compare arrays, you get an array. Numpy is refusing to interpret the results of those comparisons as a boolean.
>>> c[0] == c[0]
array([ True, True, True, True, True], dtype=bool)
>>> bool(c[0] == c[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
The implementation of index
is checking such comparisons to find the index to return. Presumably it has an optimisation which checks for identity equality first which is why c.index(a)
doesn't raise an error. But in c.index(b)
it has to check if a == b
and that's when the error happens. You can write your own loop or convert all the arrays to lists first.
Post a Comment for "Python: List Of Numpy Arrays, Can't Do Index()?"