Home > Backend Development > Python Tutorial > What is the cmp function of Python dictionary? What does the cmp function do?

What is the cmp function of Python dictionary? What does the cmp function do?

乌拉乌拉~
Release: 2018-08-18 14:08:11
Original
5277 people have browsed it

In today's article, we will learn about the built-in functions in the python dictionary. In this article, I will explain What is cmp in python, and pythoncmp()Method for parsing. Okay, without further ado, let’s get started with the article.

Description

The cmp() function of Python dictionary is used to compare two dictionary elements.

Syntax

cmp() method syntax:

cmp(dict1, dict2)
Copy after login

Parameters

dict1 - - Dictionary for comparison.

dict2 -- Dictionary to compare.

Return value

If the elements of the two dictionaries are the same, return 0, if dictionary dict1 is greater than dictionary dict2, return 1, if dictionary dict1 is less than dictionary dict2, return -1.

Example

The following example shows how to use the cmp() function:

# !/usr/bin/python
# -*- coding: UTF-8 -*-

dict1 = {'Name': 'Zara', 'Age': 7};
dict2 = {'Name': 'Mahnaz', 'Age': 27};
dict3 = {'Name': 'Abid', 'Age': 27};
dict4 = {'Name': 'Zara', 'Age': 7};
print "Return Value : %d" % cmp(dict1, dict2)
print "Return Value : %d" % cmp(dict2, dict3)
print "Return Value : %d" % cmp(dict1, dict4)
Copy after login

The output result of the above example is:

Return Value : -1
Return Value : 1
Return Value : 0
Copy after login

The above is all the content of this article, the Python built-in function cmp method. I hope what I said and the examples I gave can be helpful to you.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of What is the cmp function of Python dictionary? What does the cmp function do?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template