Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

python - Check for array - is value contained in another array?

I'd like to return a boolean for each value in array A that indicates whether it's in array B. This should be a standard procedure I guess, but I can't find any information on how to do it. My attempt is below:

A = ['User0','User1','User2','User3','User4','User0','User1','User2','User3'
     'User4','User0','User1','User2','User3','User4','User0','User1','User2'
     'User3','User4','User0','User1','User2','User3','User4','User0','User1'
     'User2','User3','User4','User0','User1']
B = ['User3', 'User2', 'User4']
contained = (A in B)

However, I get the error:

ValueError: shape mismatch: objects cannot be broadcast to a single shape

I'm using numpy so any solution using numpy or standard Python would be preferred.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use in1d I believe -

np.in1d(A,B)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...