I have a data-array like:
JavaScript
x
2
1
all = [[1,-1], [1,0], [1,1], [2,-1], [2,0], [2,1]] etc.
2
It has about 4000 pairs in it, but typically fewer on the order of a few hundred.
I need to find whether a two-valued array already exists in this large data-array, eg.
does [1,1]
exist in the array?
So the function I need should act something like this:
JavaScript
1
6
1
isValid( all, [1,1] )
2
>>> True
3
4
isValid( all, [1,100] )
5
>>> False
6
I couldn’t get the numpy functions isin()
or in1d()
to do this for me. The one function I did find works, for lists, is:
JavaScript
1
3
1
all.index( [1,1] )
2
>> True
3
but when the arg is not in the all
array, I have to try/catch a ValueError
and then return False
– acceptable for now, but not ideal.
Advertisement
Answer
You can use simple array lookup like this:
JavaScript
1
5
1
a = [[1,-1], [1,0], [1,1], [2,-1], [2,0], [2,1]]
2
3
[2,0] in a # True
4
[2,3] in a # False
5
or
JavaScript
1
3
1
a.index([2,0]) # result: 4
2
a.index([3,5]) # throw error, use try catch
3