Sort Big File Help

mk mrkafk at gmail.com
Wed Mar 3 22:46:53 CET 2010


John, there's an error in my program, I forgot that list.sort() method 
doesn't return the list (it sorts in place). So it should look like:

#!/usr/bin/python

def sortit(fname):
     fo = open(fname)
     linedict = {}
     for line in fo:
         key = line[:3]
         linedict[key] = line
     sortedlines = []
     keys = linedict.keys()
     keys.sort()
     for key in keys:
         sortedlines.append(linedict[key])
     return sortedlines

if __name__ == '__main__':
     sortit('testfile.txt')


MRAB's solution is obviously better, provided you know about Python's 
lambda.

Regards,
mk




More information about the Python-list mailing list