How to search a list of arrays How to search a list of arrays numpy numpy

How to search a list of arrays


The good question is in fact how l.index[a] can return a correct value. Because numpy arrays treat equality in a special manner: l[1] == b returns an array and not a boolean, by comparing individual values. Here it gives array([ True, True], dtype=bool) which cannot be directly converted to a boolean, hence the error.

In fact, Python uses rich comparison and specifically PyObject_RichCompareBool to compare the searched value to every element of the list is sequence, that means that it first test identity (a is b) and next equality (a == b). So for the first element, as a is l[0], identity is true and index 0 is returned.

But for any other element, identity with first element is false, and the equality test causes the error. (thanks to Ashwini Chaudhary for its nice explaination in comment).

You can confirm it by testing a new copy of an array containing same elements as l[0]:

d = array([0,1])l.index(d)

it gives the same error, because identity is false, and the equality test raises the error.

It means that you cannot rely on any list method using comparison (index, in, remove) and must use custom functions such as the one proposed by @orestiss. Alternatively, as a list of numpy arrays seems hard to use, you should considere wrapping the arrays:

>>> class NArray(object):    def __init__(self, arr):        self.arr = arr    def array(self):        return self.arr    def __eq__(self, other):        if (other.arr is self.arr):            return True        return (self.arr == other.arr).all()    def __ne__(self, other):        return not (self == other)>>> a = array([0, 1])>>> b = array([1, 0])>>> l = [ NArray(a), NArray(b) ]>>> l.index(NArray(a))0>>> l.index(NArray(b))1


This error comes from the way numpy treats comparison between array elements see : link,

So I am guessing that since the first element is the instance of the search you get the index for it, but trying to compare the first element with the second you get this error.

I think you could use something like:

[i for i, temp in enumerate(l) if (temp == b).all()]

to get a list with the indices of equal arrays but since I am no expert in python there could be a better solution (it seems to work...)