problem with global var

Peter Otten __peter__ at web.de
Fri Jan 4 05:33:21 EST 2008


Bruno Ferreira wrote:

> I wrote a very simple python program to generate a sorted list of
> lines from a squid access log file.

Now that your immediate problem is solved it's time to look at the heapq
module. It solves the problem of finding the N largest items in a list
much more efficiently. I think the following does the same as your code:

import heapq

def key(record):
    return int(record[4])

logfile = open("squid_access.log", "r")
records = (line.split() for line in logfile)
topsquid = heapq.nlargest(50, records, key=key)

for record in topsquid:
    print record[4]

Peter



More information about the Python-list mailing list