An analysis of the comparison principle of Python's built-in comparison function CMP

Source: Internet
Author: User

CMP (x, y): Compares 2 objects, which are less than the latter return-1, and equal returns 0, greater than the latter returning 1.

Comparison principle of CMP comparison function in Python

The CMP functions of Python can be compared between the same type, or between different data types. The comparison size is then determined based on the return value of the Python CMP comparison function.

>>> List1, list2 = [123, ' XYZ '], [456, ' ABC ']>>> CMP (List1, LIST2) -1>>> cmp (List2, List1) 1> >> list3 = list2 + [789]>>> list3[456, ' abc ', 789]>>> cmp (LIST2, LIST3)-1

Observing several comparisons of CMP functions above can be summarized as follows:

Python's CMP comparison function, if we compare two similar objects, the comparison operation is very intuitive.

A comparison of numbers and strings that directly compares their values.

There are similarities in the way the sequence types are compared. Python tries to make fair results when two objects are basically not comparable. For example, when two objects are not related, or if two types do not have a function for comparison at all, Python can only make a conclusion based on "logic". Algorithmic logic to compare:

1. Compare the elements of the two list.

2. If the element being compared is of the same type, the value is compared and the result is returned.

3. If two elements are not of the same type, check whether they are numbers.

A. If it is a number, perform the necessary number to force the type conversion, and then compare.

B. If one of the elements is a number, the other party's element is "large" (the number is the "smallest")

C. Otherwise, the comparison is made by the alphabetical order of the type name.

4. If one list first reaches the end, the other longer list is "large".

5. If we run out of two list elements and all the elements are equal, then the result is a draw,

is said to return a 0.

This is the CMP function comparison principle of Python.

  • 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.