Is there a more elegant way to do this?

Mike Fletcher mfletch at tpresence.com
Wed Sep 13 20:49:09 CEST 2000

```Untested, but should give you an idea...

def runningReport( self ):
set = map( None, self.counters, range(len(self.counters)))
set.sort() # sort by counters, then by index
return map( lambda x: x[1], set[-5:] ) # return just indicies

HTH,
Mike

-----Original Message-----
From: Larry Whitley [mailto:ldw at us.ibm.com]
Sent: Wednesday, September 13, 2000 2:23 PM
To: python-list at python.org
Subject: Is there a more elegant way to do this?

Here's the problem:

I have a list of counters that will have a wide variety of different values
in them.  At intervals while the program runs, I will print out the indexes
of the counters with the five largest counts.  The counters are in a list
identified below as self.counters.  Here's my inelegant way of doing it.

def runningReport(self): # a method of a larger class
temp = [] # to make sure that temp is not just another reference to
self.counters
temp = temp + self.counters # there are 100 individual counts in
self.counters
temp.sort()
temp.reverse() # now largest value is first
temp2 = [] # for the result
for i in range( 5 ):
temp2.append( self.counters.index( temp[i] ) # find the index of the
next (largest) counter and store it in temp2
print temp2

Surely there is a better way....

Larry

--
http://www.python.org/mailman/listinfo/python-list

```