[BangPypers] How to sort the IP(s)

Anand Chitipothu anandology at gmail.com
Thu May 8 11:41:21 CEST 2008


On Thu, May 8, 2008 at 2:47 PM, Anand Balachandran Pillai
<abpillai at gmail.com> wrote:
> Do you really need any kind of additional processing ?
>
>  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']

What about this?

>>> ips = ['192.168.20.1', '192.168.1.1', '172.18.13.2', '27.118.13.2']
>>> sorted(ips)
['172.18.13.2', '192.168.1.1', '192.168.20.1', '27.118.13.2']

You need to sort them as tuples of 4 numbers not as strings.


More information about the BangPypers mailing list