[Numpy-discussion] Find the N maximum values and corresponding indexes in an array

David Warde-Farley dwf@cs.toronto....
Wed Dec 2 18:15:25 CST 2009

On 2-Dec-09, at 6:55 PM, Howard Chong wrote:

> def myFindMaxA(myList):
>    """implement finding maximum value with for loop iteration"""
>    maxIndex=0
>    maxVal=myList[0]
>    for index, item in enumerate(myList):
>        if item[0]>maxVal:
>            maxVal=item[0]
>            maxIndex=index
>    return [maxIndex, maxVal]
> My question is: how can I make the latter version run faster? I  
> think the
> answer is that I have to do the iteration in C.

def find_biggest_n(myarray, n):
	ind = np.argsort(myarray)
	return ind[-n:], myarray[ind[-n:]]


More information about the NumPy-Discussion mailing list