测试给定数组的子数组 [英] Test if subarray of given array

查看:66
本文介绍了测试给定数组的子数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自Numpy初学者的一个基本问题:我有一个由5行2列组成的2D数组,您可以看到它是10个2d向量,我想测试给定向量是否在其中桌子.

this is a somewhat basic question from a beginner Numpy user: I have a 2D array of say 5 rows and 2 columns, you can see that as 10 2d-vectors, and I want to test if a given vector is inside the table.

例如:

>>> tableau = array(range(10), dtype = uint8)
>>> tableau.shape = (5,2)
>>> print tableau
[[ 0  1]
 [ 2  3]
 [ 4  5]
 [ 6  7]
 [ 8  9]]
>>> [0, 1] in tableau
True

最后一行也为True,但也为'[0,2] in tableau'.

Last line gives True, but '[0, 2] in tableau' too.

目前,我计算欧几里德距离是否为0,但是我敢肯定有一个更简单的答案.

For the moment, I compute if euclidean distance is 0, but I'm sure there's a simpler answer.

感谢您的帮助

推荐答案

您可以在match数组上执行布尔归约:

You can perform boolean reduction on the match array:

([0, 1] == tableau).all(axis=1).any()

这篇关于测试给定数组的子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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