RE: Help with sorting


Subject: RE: Help with sorting
From: Troy Melhase (tmelhase@gci.com)
Date: Tue Feb 24 2004 - 16:28:12 AKST


> I've never looked too closely at Python before,

I suggest you do. Python programming is a joy.

> but it didn't take many lines at all to get what you wanted out of it.

Which is one of the primary reasons we've adopted it so strongly at gci.net.
Well, one of the reasons I've adopted it anyway. :D

Too bad the first cut didn't work. I found two bugs, but they're fixed (and
tested!). Jon, try this instead if you're so inclined:

#!/usr/bin/env python

datafile = file('testfile')
data = datafile.read()
datafile.close()

map = {}
lines = [eval(line)[0] for line in data.split('\n') if line]
for host, foo, index in lines:
    map[int(index)] = host, foo

map = [(k, v, d) for k, (v, d) in map.items()]
map.sort()

for index, host, foo in map:
    print "('%s', '%s', '%s')" % (host, foo, index, )

Given the sample you provided:

    troy@whiterabbit tmp $ cat testfile
    ('C952tenblpl.zap2it.com', 'TENBLPL', '952'),
    ('C953tenblox.zap2it.com', 'TENBLOX', '953'),
    ('C99oln.zap2it.com', 'OLN', '99'),
    ('C9loor009.zap2it.com', 'LOOR009', '9'),

You can save and execute this file as 'test.py'. I get these results:

    troy@whiterabbit tmp $ python test.py
    ('C9loor009.zap2it.com', 'LOOR009', '9')
    ('C99oln.zap2it.com', 'OLN', '99')
    ('C952tenblpl.zap2it.com', 'TENBLPL', '952')
    ('C953tenblox.zap2it.com', 'TENBLOX', '953')
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.



This archive was generated by hypermail 2a23 : Tue Feb 24 2004 - 16:28:22 AKST