python数组部分元素的和_python:检查一个numpy数组是否包含另一个数组的任何元素...

What is the best way to check if an numpy array contains any element of another array?

example:

array1 = [10,5,4,13,10,1,1,22,7,3,15,9]

array2 = [3,4,9,10,13,15,16,18,19,20,21,22,23]`

I want to get a True if array1 contains any value of array2, otherwise a False.

解决方案

Using Pandas, you can use isin:

a1 = np.array([10,5,4,13,10,1,1,22,7,3,15,9])

a2 = np.array([3,4,9,10,13,15,16,18,19,20,21,22,23])

>>> pd.Series(a1).isin(a2).any()

True

And using the in1d numpy function(per the comment from @Norman):

>>> np.any(np.in1d(a1, a2))

True

For small arrays such as those in this example, the solution using set is the clear winner. For larger, dissimilar arrays (i.e. no overlap), the Pandas and Numpy solutions are faster. However, np.intersect1d appears to excel for larger arrays.

Small arrays (12-13 elements)

%timeit set(array1) & set(array2)

The slowest run took 4.22 times longer than the fastest. This could mean that an intermediate result is being cached

1000000 loops, best of 3: 1.69 µs per loop

%timeit any(i in a1 for i in a2)

The slowest run took 12.29 times longer than the fastest. This could mean that an intermediate result is being cached

100000 loops, best of 3: 1.88 µs per loop

%timeit np.intersect1d(a1, a2)

The slowest run took 10.29 times longer than the fastest. This could mean that an intermediate result is being cached

100000 loops, best of 3: 15.6 µs per loop

%timeit np.any(np.in1d(a1, a2))

10000 loops, best of 3: 27.1 µs per loop

%timeit pd.Series(a1).isin(a2).any()

10000 loops, best of 3: 135 µs per loop

Using an array with 100k elements (no overlap):

a3 = np.random.randint(0, 100000, 100000)

a4 = a3 + 100000

%timeit np.intersect1d(a3, a4)

100 loops, best of 3: 13.8 ms per loop

%timeit pd.Series(a3).isin(a4).any()

100 loops, best of 3: 18.3 ms per loop

%timeit np.any(np.in1d(a3, a4))

100 loops, best of 3: 18.4 ms per loop

%timeit set(a3) & set(a4)

10 loops, best of 3: 23.6 ms per loop

%timeit any(i in a3 for i in a4)

1 loops, best of 3: 34.5 s per loop

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值