как отобразить индекс строки и столбца в следующем коде - PullRequest
1 голос
/ 16 мая 2019

Должны отображать элементы в соответствии с их строками и столбцами.

import numpy as np
b = np.arange(9.).reshape(3, 3)
print(b)
b5 = np.where( b >5)
c=b[b5]

print("***Values of where the elements are, and their rows/columns***")
for row,col in enumerate(b):
    for j in c:
        if col in c:
            print("Value:",j,"in row",row,", column",col)
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]

***Values of where the elements are, and their rows/columns***
Value: 6.0 in row 2 , column [6. 7. 8.]
Value: 7.0 in row 2 , column [6. 7. 8.]
Value: 8.0 in row 2 , column [6. 7. 8.]

Ожидается, что будет показан номер столбца. то есть. Значение: 6,0 в строке 2, столбец 0

1 Ответ

0 голосов
/ 16 мая 2019

Индексы в b5 вам просто нужно извлечь их:

for row, col, val in zip(*b5, c):
   print("row", row, "col", col, "val", val)

печать:

row 2 col 0 val 6.0
row 2 col 1 val 7.0
row 2 col 2 val 8.0
...