ssb
ssb

Reputation: 7502

Finding Minimum in Python Arrays

I have two arrays say x = [110, 10, 1000 ....] and y = ['adas', 'asdasqe', 'ae1e' ....]

Both of these arrays are of the same length. My problem is that or printing the 10 values of y such that the corresponding values of x are the 10 largest.

In an average test case, x and y are 4000-5000 in length. So speed is of the essence. Could you tell me a way of doing this using some of the inbuilt functions of python so that the operation is as fast as possible.

Upvotes: 2

Views: 110

Answers (1)

eumiro
eumiro

Reputation: 212835

If you want the ten top elements from a list of several thousands, you can try heapq:

import heapq

heapq.nlargest(10, zip(x, y))

Upvotes: 7

Related Questions