George Sha - 5 months ago 8

Python Question

For example, I have a 2-dimensional matrix:

`[0 0 0 0 5 0 0 0`

0 5 0 0 5 0 0 0

0 0 5 5 5 5 5 0

5 0 0 0 5 0 0 5

0 5 0 0 0 0 5 0]

I want to get the indexes of the row and column with the most 5s. Which should be the third row and fifth column. I don't want to use for loop to get that because it cost too much time; is there anyway to solve it using numpy module in python?

Answer

```
colnum = np.argmax([list(a[:,x]).count(5) for x in range(a.shape[1])])
colnum
Out[22]: 4
rownum = np.argmax([list(a[x,:]).count(5) for x in range(a.shape[0])])
rownum
Out[25]: 2
```

Number starts from 0