This section briefly introduces the use of the cmp () method in Python2.x, and python2.xcmp
The cmp () method compares the elements of the two lists.
Syntax
The syntax of the cmp () method is as follows:
Cmp (list1, list2)
Parameters
- List1 -- this is the first list to be compared
- List2 -- this is the second list to be compared
Return Value
If the elements are of the same type, compare them and return results. If the elements are of different types, check to see if they are numbers.
- Force numeric comparison if necessary
- If either element is a number and the other element is "Big" (the number is "minimum ")
- Otherwise, the type is sorted alphabetically by name
If the end of one in the list is reached, the long list is "large ". If the list is exhausted and the same data is shared, the result is parallel, which means that 0 is returned.
Example
The following example shows the use of the cmp () method.
#! /Usr/bin/pythonlist1, 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 will produce the following results:
-11-1