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)
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)
The output result of the above example is:
Return Value : -1 Return Value : 1 Return Value : 0
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!