w3resource logo
Python Exercises

Python: Get the n largest values of an array

Python NumPy: Random Exercise-16 with Solution

Write a Python program to get the n largest values of an array.

Sample Solution:-

Python Code:

import numpy as np
x = np.arange(10)
print("Original array:")
print(x)
np.random.shuffle(x)
n = 1
print (x[np.argsort(x)[-n:]])

Sample Output:

Original array:                                                        
[0 1 2 3 4 5 6 7 8 9]                                                  
[9]

Python Code Editor:

import numpy as np
x = np.arange(10)
print("Original array:")
print(x)
np.random.shuffle(x)
n = 1
print (x[np.argsort(x)[-n:]])

Improve this sample solution and post your code through Disqus