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.