[BangPypers] How to sort the IP(s)
venkata subramanian m
venkatasubramanian at gmail.com
Thu May 8 11:19:51 CEST 2008
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 <anandology at gmail.com> wrote:
>
>> On Thu, May 8, 2008 at 11:53 PM, Kushal Das <kushaldas at gmail.com> 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
>> BangPypers at python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>>
>>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20080508/73ce682b/attachment-0001.htm>
More information about the BangPypers
mailing list