Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
a = np.array([1,2,4,2,3,4,1]) s = [1,2]
How can I get an array which tells me whether the elements in s exist in a? This is what I'm hoping to get:
s
a
[True, True, False, True, False, False, True]
Use np.isin
np.isin
>>> np.isin(a,s) array([ True, True, False, True, False, False, True])
Add a comment
no numpy, you can do like this
a = [1,2,4,2,3,4,1] s = [1,2] t = list(map(lambda a: a in s, a))
if is s is larger, set is more effective
set
a = [1,2,4,2,3,4,1] s = set([1,2]) t = list(map(lambda a: a in s, a))
import numpy as np a = np.array([1, 2, 4, 2, 3, 4, 1]) s = [1, 2] r = [n in s for n in a] print(r) # [True, True, False, True, False, False, True]
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.