Anand Balachandran Pillai wrote:
Do you really need any kind of additional processing ?
But, string sorting might not produce proper sorting right?
>>> l=['192.168.11.1','172.18.13.2','192.168.2.2','172.19.2.1']
>>> l.sort()
>>> l
['172.18.13.2', '172.19.2.1', '192.168.11.1', '192.168.2.2']
But,
['172.18.13.2', '172.19.2.1', '192.168.2.2', '192.168.11.1']
is the expected order, I guess.
Regards,
Venkat
The basic sort algorithm is smart enough to do this by itself,
l=['192.168.1.1','172.18.13.2','192.168.3.2','172.19.2.1']
l.sort()
l
['172.18.13.2', '172.19.2.1', '192.168.1.1', '192.168.3.2']
or use sorted(...) if you don't want to modify in place...
Here is an even closer example to demo this...
l=['192.168.12.21','192.168.12.15','192.168.11.10','192.168.10.5','192.168.15.1','192.167.10.1']
sorted(l)
['192.167.10.1', '192.168.10.5', '192.168.11.10', '192.168.12.15',
'192.168.12.21', '192.168.15.1']
--Anand
On Thu, May 8, 2008 at 2:41 PM, Anand Chitipothu <[EMAIL PROTECTED]> wrote:
On Thu, May 8, 2008 at 11:53 PM, Kushal Das <[EMAIL PROTECTED]> wrote:
Hi,
> What is the best way to sort IP numbers
> numbers like
> 192.168.20.1
> 192.168.1.1
> 172.18.13.2
ips = ['192.168.20.1', '192.168.1.1', '172.18.13.2']
sorted(ips, key=lambda ip: [int(x) for x in ip.split('.')])
# ips.sort(key=lambda ip: [int(x) for x in ip.split('.')]) if you want
to sort in-place.
_______________________________________________
BangPypers mailing list
[email protected]
http://mail.python.org/mailman/listinfo/bangpypers
_______________________________________________
BangPypers mailing list
[email protected]
http://mail.python.org/mailman/listinfo/bangpypers