python - numpy where:如何通过匹配一维数组在 3D 数组中找到 2D 数组的索引?
问题描述
a = np.array([[[1,2,3], (1,1,0,0,8)], [[1,2,3], (1,1,2,0,8)], [[1,2,3], (4,1,0,0,8)]])
where = np.where(a[:,1] == (1,1,0,0,8))
print(where)
输出:
(array([], dtype=int64),)
我希望它输出出现0
的行的索引(1,1,0,0,8)
解决方案
NumPy 通常将元组解释为数组或数组的子集而不是元素,但您可以通过以下方式绕过它:
el = np.array([None], dtype = object)
el[0] = (1,1,0,0,8)
where = np.where( a[:,1] == el ) # outputs a tuple: (array([0], dtype=int64),)
推荐阅读
- ios - 在 SwiftUI 中呈现自定义视图时如何忽略导航栏和标签栏?
- c# - 转换字典
> 进入列表 - c# - 在 EF Core Fluent 配置中使用 SqlServer HASHBYTES
- api - Kubernetes 通过 api 自动缩放
- swift - SwiftUI:如何将列表中的项目拖放到相同(另一个)列表中的另一个项目中
- java - 加载图像时,JProgressBar 不会在 for 循环中更新
- javascript - JS中未声明的变量等于getElementById?
- azure - 为什么通过内存流上传 JSON 时 BlobClient.UploadAsync 会挂起?
- javascript - data.map 函数在第二次调用后停止工作
- r - tidyr 1.1.0 中的 unnest 错误错误:无法分配大小为 1024 Kb 的向量