Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I index the 3 highest values in a list?

Tags:

python

so i have these 2 lists:

score = [350, 914, 569, 223, 947, 284, 567, 333, 697, 245, 227, 785, 120, 794, 343, 773, 293, 995] name = [Ryan, Stacy, Jenna, Peter, Sophie, Bryan, Cole, Andrea, Emily, Blake, Mike, Stephan, Rob, Eliza, Heather, Daniel, Elisabeth, Samantha] 

I have to find the 3 highest scores and tally these scores with their respective scorers in the name list, so that I can have new lists

top3score = [947, 995, 914] top3name = [Sophie, Samantha, Stacy] 

I'm thinking of indexing the highest scores, appending them into a list than using the index to tally these scores with the names.

my question is how do i index the 3 highest values in the list? and then, how do i use the index to look for the scorers name in the name list so that i can append them in the top3name list?

like image 356
rudster Avatar asked Apr 14 '12 08:04

rudster


People also ask

How do you find the top 3 values in Python?

If you want to get the indices of the three largest values, you can just slice the list. It also supports sorting from smallest to largest by using the parameter rev=False .

How do you find the index of the highest number in a list?

index() functions to find out the index of the maximum value in a list. Use the enumerate() function to find out the index of the maximum value in a list. Use the numpy. argmax() function of the NumPy library to find out the index of the maximum value in a list.

How do you find an index of a value in a list?

To find the index of an element in a list, you use the index() function. It returns 3 as expected. However, if you attempt to find an element that doesn't exist in the list using the index() function, you'll get an error.

How do you find the highest value in a list in Python?

In Python, there is a built-in function max() you can use to find the largest number in a list. To use it, call the max() on a list of numbers. It then returns the greatest number in that list.


2 Answers

I think this will do it

sorted(zip(score, name), reverse=True)[:3] 

So you understand what is going on:

zip: takes iterables as it's arguments and takes one element from each iterable, placing them in a tuple.

So:

>>> zip(score, name) [(350, 'Ryan'), (914, 'Stacy'), (569, 'Jenna'), (223, 'Peter'), (947, 'Sophie'), (284, 'Bryan'), (567, 'Cole'), (333, 'Andrea'), (697, 'Emily'), (245, 'Blake'), (227, 'Mike'), (785, 'Stephan'), (120, 'Rob'), (794, 'Eliza'), (343, 'Heather'), (773, 'Daniel'), (293, 'Elisabeth'), (995, 'Samantha')] 

sorted: will sort the data. By default, a tuple element is sorted on the element in the 0 index, so the score in this case. Reverse=True will sort it descending first.

And lastly, the [:3] is slice notation, saying give me all elements from the beginning up to the 3rd element. This could have also been written as [0:3]

like image 128
sberry Avatar answered Sep 25 '22 21:09

sberry


If you're only interested on the top 3, there'sheapq.nlargest:

>>> heapq.nlargest(3, zip(score, name)) [(995, 'Samantha'), (947, 'Sophie'), (914, 'Stacy')] 

From the official doc:

heapq.nlargest(n, iterable, key=None)

Return a list with the n largest elements from the dataset defined by iterable. key, if provided, specifies a function of one argument that is used to extract a comparison key from each element in the iterable: key=str.lower Equivalent to: sorted(iterable, key=key, reverse=True)[:n]

Performance notice:

The latter two [nlargest and nsmallest] perform best for smaller values of n. For larger values, it is more efficient to use the sorted() function. Also, when n==1, it is more efficient to use the built-in min() and max() functions.

like image 27
Rik Poggi Avatar answered Sep 25 '22 21:09

Rik Poggi