Reputation: 169
like this:
>> arr = np.array([[0, 50], [100, 150], [200, 250]])
>>> values = [100, 200, 300]
>>> arr in values
expect:
array([[False, False],
[ True, False],
[ True, False]])
result:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I wrote following code and it works, but this code cannot accept changing length of list
(arr==values[0]) | (arr==values[1]) | (arr==values[2])
Upvotes: 1
Views: 972
Reputation: 1092
This should work but only for 2 level depth:
import numpy as np
def is_in(arr, values):
agg = []
for a in arr:
if isinstance(a, list):
result = is_in(a, values)
agg.append(result)
else:
result = np.isin(arr, values)
return result
return agg
arr = np.array([[0, 50], [100, 150], [200, 250]])
values = [100, 200, 300]
print(is_in(arr, values))
Upvotes: 0
Reputation: 98
Use np.isin:
import numpy as np
arr = np.array([[0, 50], [100, 150], [200, 250]])
values = [100, 200, 300]
np.isin(arr, values)
result:
array([[False, False],
[ True, False],
[ True, False]])
Upvotes: 4
Reputation: 182
also change name of that variable from values to something different to valuess for example.
if valuess in arr.values :
print(valuess)
OR
Use a lambda function.
Let's say you have an array:
nums = [0,1,5]
Check whether 5 is in nums:
(len(filter (lambda x : x == 5, nums)) > 0)
Upvotes: -3