Is there a more elegant way to do this?

Opinderjit bhellao at my-deja.com
Wed Sep 13 17:45:31 EDT 2000


Will this work for you?

def runningReport(self):
    cnt = self.counters[:]  # make a copy
    cnt.sort()
    indx = cnt[len(cnt)-5:] # get the 5 largest

    for i in indx:
        print self.counters(cnt[i])

In article <8poglt$10fq$1 at news.rchland.ibm.com>,
  "Larry Whitley" <ldw at us.ibm.com> wrote:
> 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
>
>

--
Java who?


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list