Is there a more elegant way to do this?

Aahz Maruch aahz at panix.com
Wed Sep 13 19:12:18 EDT 2000


In article <8pou8g0tbt at news1.newsguy.com>,
Alex Martelli <aleaxit at yahoo.com> wrote:
>"Aahz Maruch" <aahz at panix.com> wrote in message
>news:8poiii$999$1 at panix3.panix.com...
>> In article <8poglt$10fq$1 at news.rchland.ibm.com>,
>> Larry Whitley <ldw at us.ibm.com> wrote:
>>>
>>>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
>    [snip]
>> def runningReport(self, numItems=5): # a method of a larger class
>>     if numItems > len(self.counter):
>>         numItems = len(self.counter)
>>     numItems = -1 * abs(int(numItems)) # make sure numItems is negative int
>>     temp = self.counters[:] #make copy of self.counters
>>     temp.sort()
>>     temp2 = temp[numItems:] #negative numItems pulls from end of list
>>     temp2.reverse() #Change to descending order?
>>     print temp2
>
>It seems to me that you're printing the VALUES, not the INDEXES,
>of the five largest counters.  Printing the indexes was the request.

Um, yeah.  My bad for reading his code instead of his request.  ;-)

>The basic approach I would suggest: first, place in temp a list of
>PAIRS (countervalue,counterindex); then, sort (on countervalue);
>the, for the last numItems items, print the second of each pair.

Yup.  
-- 
                      --- Aahz (Copyright 2000 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

"I'm not aware of any public servant on Usenet, and thank God for that."  --rra



More information about the Python-list mailing list