检查数组 - 值是否包含在另一个数组中? [英] Check for array - is value contained in another array?

查看:39
本文介绍了检查数组 - 值是否包含在另一个数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为数组 A 中的每个值返回一个布尔值,指示它是否在数组 B 中.我猜这应该是一个标准程序,但我找不到有关如何执行此操作的任何信息.我的尝试如下:

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)

但是,我收到错误:

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

我使用的是 numpy,因此任何使用 numpy 或标准 Python 的解决方案都是首选.

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

推荐答案

您可以使用 in1d 我相信 -

You can use in1d I believe -

np.in1d(A,B)

这篇关于检查数组 - 值是否包含在另一个数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆