2017-08-16 116 views
0
000000000 
000050000 
000505000 
005000500 
050000050 
500040005 
050000050 
005000500 

在本例中,我想找到它們是同一行4.我已經有4的保存爲centre索引在5秒的索引。Numpy.where與邏輯

期望的輸出是[[5,0],[5,8]]

我嘗試這一點,但它不工作:

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

回答

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]] 
0

通過做指數= ZIP(* np.where(陣列[中心[解決它0],::] == 5))