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])
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]])
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))
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.