string compare question.Please Help!!!

Sean 'Shaleh' Perry shalehperry at attbi.com
Mon Dec 24 22:54:15 EST 2001


On 25-Dec-2001 ÕÅÉÙ³Û wrote:
> I have question about cmp function,follows:
> $python
> Python 2.2b1 (#1, Nov 21 2001, 10:24:36)
> [GCC 2.96 20000731 (Red Hat Linux 7.0)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> 
>>>>cmp('10.66.73.78','10.0.0.0')
> 1 
>#the result 1 is my desire
> 
> 
>>>>cmp('10.66.73.78','10.255.255.255')
> 1  
>#the result isn't my desire,I want to 
>#make '10.66.73.78' is less than '10.255.255.255',
>#I want to get 0,but it return 1
> 
> Where is my wrong? How to correct it? Thanks.
> Edward

This is a string compare, so each char is compared to the char in the other
string. 10 == 10, so then 66 > 0, 73 > 0, etc.  In the second test the opposite
is true.

What you want to happen will require a special function.  One which knows about
the rules of IP addresses (which those look like).

A simple guess would be split the value on '.', then compare the items as
numbers while catching the 255 case.




More information about the Python-list mailing list