簡體   English   中英

Numpy.where與邏輯

[英]Numpy.where with logic

000000000
000050000
000505000
005000500
050000050
500040005
050000050
005000500

在此示例中,我想找到與4處於同一行的5s的索引。我已經將4的索引保存為centre

所需的輸出是[[5,0],[5,8]]

我嘗試了此操作,但不起作用:

index = zip(*np.where(array== 5 and x == centre[0]))

列表理解的解決方案:

arr = ['000000000',
       '000050000',
       '000505000',
       '005000500',
       '050000050',
       '500040005',
       '050000050',
       '005000500']

indices = [[j, i] 
           for j, row in enumerate(arr)
           for i, val in enumerate(row) 
           if '4' in row and val=='5']

print(indices)
# [[5, 0], [5, 8]]

通過執行index = zip(* np.where(array [centre [0] ,:] == 5)來解決它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM