sorting on IP addresses
Max Møller Rasmussen
maxm at normik.dk
Wed Feb 7 03:33:26 EST 2001
From: Sam Wun [mailto:swun at esec.com.au]
>Does anyone know what is the quickly way to sort a list of IP addresses?
>ie. 203.21.254.89 should be larger than 203.21.254.9
it probably doesn't get any quikclier than this:
-----------------------------------
from string import split, join
ips = """
192.168.1.1
192.168.1.2
192.168.1.3
192.169.1.5
127.1.2.3
192.167.1.6
191.168.1.4
193.168.1.2
127.1.1.1
194.168.1.8
195.168.1.2
"""
ipList = split(ips, '\n')
ipTupleList = []
for ip in ipList:
if ip != '': # no empty lines
n1, n2, n3, n4 = tuple(split(ip, '.'))
ipTupleList.append((int(n1), int(n2), int(n3), int(n4)))
ipTupleList.sort()
for ipTuple in ipTupleList:
print "%s.%s.%s.%s" % ipTuple
-----------------------------------
Regards Max M
More information about the Python-list
mailing list