Use of python-cmp ()

Source: Internet
Author: User

NOTE: The CMP () function has been removed from the Python3.

CMP (x, y)

Compares x with Y when x>y, returns 1 when X==y, returns 0 when X<y, returns 1;

1&GT;&GT;&GT;CMP (1, 0)213&GT;&GT;&GT;CMP ()4 05&GT;&GT;&GT;CMP ()6-17&GT;&GT;&GT;CMP ('a','b')8-19&GT;&GT;&GT;CMP ('ABC','AB')Ten1 One #because ' ABC ' is longer than ' AB ', so determine ' abc ' > ' AB '

Special Places:

1. If the types of comparisons are different, but both are numbers (shaping and floating-point types), then it is necessary to make a forced number conversion (a bit of a question about which type to cast, but it does not matter);

2. If x is a number, Y is not a number, then X<y (the number is the smallest);

Print true = = 1trueprint true = = 0Falseprint false = = 1falseprint false = = 0True#Thetrue and false in the bool type are also represented by 1 and 0 in the computer

3. When x, y are not a number and the type is different, the name of its type (String,bool ...) to judge;

>>> CMP (True,'true')-1>>> cmp (False,'True  ')-1

The above is a simple comparison of two data, the following is a comparison list.

>>> List1 = [a]>>> List2 = [a]>>>CMP (LIST1,LIST2) 0>>> list2[2] = 4>>>CMP (LIST1,LIST2)-1>>> List1 = ['ABC','a']>>> List2 = ['ABC', 1]>>>CMP (LIST1,LIST2)1>>> List2[1] ='b'>>>CMP (LIST1,LIST2)-1>>> List2[1] =True>>>CMP (LIST1,LIST2)1

Use of python-cmp ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.