Issue
x = np.array([3, 4, 2, 1, 7, 8, 6, 5, 9])
I want to get an answer as array([9,8,7,6,5])
and their indices array([8,5,4,6,7])
.
I've tried np.amax
which only provides a single value.
Solution
You can do this (each step is commented for clarity):
import numpy as np
x = np.array([3, 4, 2, 1, 7, 8, 6, 5, 9])
y = x.copy() # <----optional, create a copy of the array
y = np.sort(x) # sort array
y = y[::-1] # reverse sort order
y = y[0:5] # take a slice of the first 5
print(y)```
The result:
[9 8 7 6 5]
Answered By - D.L
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.