This article mainly introduces the use of the CMP () method in the python2.x version, but this method is no longer built in the python3.x version ... Need friends can refer to the following
The CMP () method compares elements of two lists.
Grammar
The following is the syntax for the CMP () method:
?
Parameters
List1--This is the first list to compare
List2--This is the second list to compare
return value
If the element is of the same type, the comparison is performed and the result is returned. If the elements are different types, check to see if they are numbers
Force a numeric comparison if the number is necessary
If any element is a number, then the other element is "large" (the number is "minimal")
Otherwise, the type is sorted alphabetically by name
If you reach the end of one of the lists, the longer list is "big." If you deplete the list and share the same data, the result is tied, which means returning 0
Example
The following example shows the use of the CMP () method.
?
1 2 3 4 5 6 7 8 |
#!/usr/bin/python List1, list2 = [123, ' XYZ '], [456, ' ABC '] PRINT cmp (list1, LIST2); Print CMP (LIST2, List1); LIST3 = List2 + [786]; Print CMP (LIST2, LIST3) |
When we run the above program, it produces the following results:
?