簡體   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