检查 Python 中的 3d 数组中是否存在 2d 数组? [英] Check if 2d array exists in 3d array in Python?

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

问题描述

我有一个形状为 (1000, 12, 30) 的 3d 数组,我有一个形状为 (12, 30) 的 2d 数组的列表,我想要做的是检查这些 2d 数组是否存在于 3d 数组中.Python中有没有一种简单的方法可以做到这一点?我试过关键字 in 但它不起作用.

I have an 3d array with shape (1000, 12, 30), and I have a list of 2d array's of shape (12, 30), what I want to do is check if these 2d arrays exist in the 3d array. Is there a simple way in Python to do this? I tried keyword in but it doesn't work.

推荐答案

numpy 有一个方法,你可以用 np.all

There is a way in numpy , you can do with np.all

a = np.random.rand(3, 1, 2)
b = a[1][0]
np.all(np.all(a == b, 1), 1)
Out[612]: array([False,  True, False])

来自bnaecker的解决方案

Solution from bnaecker

np.all(a == b, axis=(1, 2))

如果只想检查是否退出

np.any(np.all(a == b, axis=(1, 2)))

这篇关于检查 Python 中的 3d 数组中是否存在 2d 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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