Finding the Index of N biggest elements in Python Array / List Efficiently

前端 未结 4 1590
时光说笑
时光说笑 2020-12-28 16:15

I\'m sorry in advance if this is a duplicated question, I looked for this information but still couldn\'t find it.

Is it possible to arrange a numpy array (or python

4条回答
  •  自闭症患者
    2020-12-28 16:57

    Have you looked at the built-in numpy argsort method?:

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html

    I can sort an array with 300,000 random floats in about 29 ms on my machine using that method.

    def f(a,N):
        return np.argsort(a)[::-1][:N]
    

提交回复
热议问题