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.
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?