I have an array with 4 randomly generated elements (using Python):
pa = np.random.normal(10, 5, 4)
I can find the largest element by doing:
ml = np.max(pa)
mll = np.where(pa == ml)
print(mll)
I'm wondering how can I find the 2nd and 3rd elements in this array? Also, my current output looks like:
(array([0]),)
Is there a way I can get a pure numerical output (0)? Thanks!
Update: Sorry for the previous confusion, I want to find the index of the 2nd and 3rd largest element, but all the answers so far are very helpful to me. Thanks!!
If you want the three largest of the four values, then you can just find all the values that are greater than the minimum value. You can use argwhere
to get just an array of indexes:
import numpy as np
pa = np.random.normal(10, 5, 4)
ml = np.min(pa)
mll = np.argwhere(pa > ml)
print(mll)
Sample output:
[[0]
[1]
[3]]
To flatten that output, convert to an array and flatten
:
mll = np.array(np.nonzero(pa > ml)).flatten()
print(mll)
Sample output:
[0 1 3]
@Zhengrong see my edit