Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if numpy array is in list of numpy arrays

Tags:

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?

like image 412
fsperrle Avatar asked Jun 01 '14 10:06

fsperrle


People also ask

How do you check if a NumPy array is in a list?

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.

How do I check if an array is in a list Python?

To check if the item exists in the list, use Python “in operator”. For example, we can use the “in” operator with the if condition, and if the item exists in the list, then the condition returns True, and if not, then it returns False.

Can a list contain NumPy arrays?

A list in Python is simply a collection of objects. These objects can be integers, floating point numbers, strings, boolean values or even other data structures like dictionaries. An array, specifically a Python NumPy array, is similar to a Python list.

Is NumPy array or list faster?

NumPy Arrays are faster than Python Lists because of the following reasons: An array is a collection of homogeneous data-types that are stored in contiguous memory locations. On the other hand, a list in Python is a collection of heterogeneous data types stored in non-contiguous memory locations.


2 Answers

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.

like image 136
gg349 Avatar answered Sep 21 '22 20:09

gg349


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 
like image 33
Moj Avatar answered Sep 20 '22 20:09

Moj