2

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.

1
  • 2
    strictly speaking, in python these are lists not arrays ;) Commented Apr 8, 2013 at 11:28

1 Answer 1

7

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.