40

I have a list of numpy arrays and a single numpy array. I want to check if that single array is a member of the list.

I suppose there exist a method and I haven't searched properly... This is what I came up with:

def inList(array, list):
    for element in list:
        if np.array_equal(element, array):
            return True
    return False

Is this implementation correct? Is there any ready function for this?

4
  • can you provide an example? Commented Jun 1, 2014 at 11:04
  • 2
    "Is this implementation correct?" Well, have you tested it?! Commented Jun 1, 2014 at 11:04
  • 2
    Evidently you are using a list of ndarrays because they can have different lengths or data types? Otherwise you would have converted your list to an array. And what do you mean by a single array being a member of the list? Does it have to be the identical or does it have to be equal to the array you are testing with? Commented Jun 1, 2014 at 11:08
  • Just for reference, a similar question is answered here, stackoverflow.com/questions/5488307/numpy-array-in-python-list Commented May 7, 2018 at 23:48

3 Answers 3

16

There is a much simpler way without the need to loop using np.all(). It only works when all the arrays within the list of arrays have the same shape:

list_np_arrays = np.array([[1., 1.], [1., 2.]])
array_to_check = np.array([1., 2.])

is_in_list = np.any(np.all(array_to_check == list_np_arrays, axis=1))

The variable is_in_list indicates if there is any array within he list of numpy arrays which is equal to the array to check.

Sign up to request clarification or add additional context in comments.

1 Comment

In case you want to check equality up to a tolerance: np.any(np.all(np.isclose(array_to_check, list_np_arrays, axis=1)))
13

Using the verb is when talking about python is a bit ambiguous. This example covers all the cases I could think of:

from __future__ import print_function
from numpy import array, array_equal, allclose

myarr0 = array([1, 0])
myarr1 = array([3.4499999, 3.2])
mylistarr = [array([1, 2, 3]), array([1, 0]), array([3.45, 3.2])]

#test for identity:
def is_arr_in_list(myarr, list_arrays):
    return next((True for elem in list_arrays if elem is myarr), False)

print(is_arr_in_list(mylistarr[2], mylistarr)) #->True
print(is_arr_in_list(myarr0, mylistarr)) #->False
#myarr0 is equal to mylistarr[1], but it is not the same object!

#test for exact equality
def arreq_in_list(myarr, list_arrays):
    return next((True for elem in list_arrays if array_equal(elem, myarr)), False)

print(arreq_in_list(myarr0, mylistarr)) # -> True
print(arreq_in_list(myarr1, mylistarr)) # -> False

#test for approximate equality (for floating point types)
def arreqclose_in_list(myarr, list_arrays):
    return next((True for elem in list_arrays if elem.size == myarr.size and allclose(elem, myarr)), False)

print(arreqclose_in_list(myarr1, mylistarr)) #-> True

PS:do NOT use list for a variable name, as it is a reserved keyword, and often leads to subtle errors. Similarly, do not use array.

1 Comment

How would you get the index of the array in the list for arreq_in_list?
7

lets say you have an array like this:

a= [array([ 1, 24,  4, 5]), array([ 22,   4, 123]), array([11,  1,  1])]
#convert all subarrays to list
a= [ list(item) for item in a ]

no you can check for a sublist like this:

In [80]: [1,22,4] in a
Out[80]: False

In [81]: [1,24,4,5] in a
Out[81]: True

2 Comments

You are assuming that all arrays are of the same shape, which is not clear from the question. Then you convert the whole list to an array. And then, for checking, you convert the whole thing back to a list of lists? This may, by a stroke of luck, work for the OPs purposes, but it doesn't seem like anything close to the optimal way of doing this.
@eickenberg well you are right about the optimal solution. this is basically he said he has a list of numpy array and for using this solution all arrays should be converted to list. so I could either iterate through nested list and convert or use above solution. about the same length array you right. I'm updating my solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.