提取包含特定值的数组索引 [英] Extract array indices that contain a specific value

查看:54
本文介绍了提取包含特定值的数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形状为(31777,44,44)

sum为31777.0,并且每个(44,44)网格在某些坐标处仅包含一个1.0条目.

Its sum is 31777.0, and every (44,44) grid contains just one 1.0 entry at some coordinates.

我的目标是获取形状为(31777,2)的数组,其中2是网格中1.0条目的坐标. 这样做的麻烦方式是循环,但我正在寻找一种优雅的高性能解决方案.

My objective is to obtain an array of shape (31777, 2) where the 2 are the coordinates of the 1.0 entry in the grid. The messy way to do this is with a loop, but I'm looking for an elegant performant solution to this.

推荐答案

第一个答案给了我Google材料,以查找我认为是最佳的:

The first answer gave me google material to find what I believe is optimal:

np.vstack(arr.nonzero()[1:][::-1]).T

这篇关于提取包含特定值的数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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