繁体   English   中英

有没有办法在二维数组中找到输入值的每个索引?

[英]Is there a way to find every index of an input value in a 2d array?

我一直在尝试在 python 中创建一个 function ,它返回二维数组中每个重复值的 x,y 坐标。 例如,如果我有数组和一个值,

array = [ [1 ,2 ,3]
          [2 ,3 ,1]
          [3 ,2, 1]]

search = 1

它会 output (0,0) (1,2) (2,2)

我一直在尝试使用一些功能,例如 np.where 或将其转换为 pandas 数据框并以这种方式进行搜索,但我不确定最好的方法。 当我使用 np.where 时,它返回一个空数组,因为我使用的是长小数。 我正在尝试在 200 x 200 的阵列上执行此操作。

我们可以做np.where PS: a 是你的数组

list(zip(*np.where(a==search)))
[(0, 0), (1, 2), (2, 2)]

正如 hpaulj 提到的

np.argwhere(np.isclose(a,search))
array([[0, 0],
       [1, 2],
       [2, 2]])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM