[Tutor] Getting the highest numbers from a list

dman dsh8290@rit.edu
Mon, 27 Aug 2001 15:12:28 -0400


On Mon, Aug 27, 2001 at 02:20:57PM -0400, David L. Lerner wrote:
| Is there a simple way to get the _n_ highest numbers from an unordered list?
| 
| I have a long list that I randomly generated.  I need the five highest
| numbers.  I have another long list, also randomly generated, from which I
| need the _sum_ of the five highest numbers

How about :

the_list = <random list>
the_list.sort()
top5 = the_list[-5:]

sum = 0
for item in top5 :
    sum += item


?

-D