简体   繁体   English

使用python从数组中查找第4个最大元素

[英]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]) . 我有一个数组,其中有一个像a = array.array('i',[3,5,7,2,8,9,10,37,99])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, 现在我必须找到第4大元素,如果这是一个list,那么我可以通过这种方式找到,

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. 您可以使用numpy.argsort ,该顺序为您提供最小值的索引。 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. 但是,如果您使用此解决方案(意味着您使用numpy)并计划对数组做更多操作,则可以考虑首先使用numpy.array而不是array.array。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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