Is there a more elegant way to do this?
glandrum at my-deja.com
Thu Sep 14 01:56:54 CEST 2000
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
> in them. At intervals while the program runs, I will print out the
> of the counters with the five largest counts. The counters are in a
> identified below as self.counters. Here's my inelegant way of doing
If you are willing to use Numeric you'll get a lot of what you want
order = argsort(self.counters)[-num:].tolist()
That's not the prettiest code, but it should be pretty fast. argsort()
is truly a wonderful thing. (Don't forget the 'from Numeric import *'
If you switch completely to Numeric, you can skip the conversions to
a list, which should produce another speedup.
Further use of Numeric features leads to:
but that's maybe a bit *too* perliffic.
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list