A brief introduction to the use of the CMP () method in the python2.x version

Source: Internet
Author: User
Tags comparison

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:

?

1 CMP (List1, List2)

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:

?

1 2 3 -1 1-1

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.