This section briefly introduces the use of the cmp () method in Python2.x, and python2.xcmp

Source: Internet
Author: User

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

Related Article

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.