简体   繁体   中英

finding 4th largest element from array using python

I have an array in which i have element like a = array.array('i',[3,5,7,2,8,9,10,37,99]) . Now I have to find 4th largest element, If this is a list , then i can find by this way,

l = [3,5,7,2,8,9,10,37,99]
m = sorted(l)
m[-4]

you could use numpy.argsort that gives you the indices of the min values in order. So:

from numpy import argsort
index_to_fourth_largest_element = argsort(a)[-4]

But if you use this solution (meaning that you use numpy) and plan to do more with the array you could considering using numpy.array instead of array.array in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM