描述
Python字典法cmp()根據鍵和值比較兩個字典。
用法
以下是語法cmp()方法 -
cmp(dict1, dict2)
參數
dict1- 這是第一個與 dict2 進行比較的字典。
dict2- 這是與 dict1 進行比較的第二個字典。
返回值
如果兩個字典相等,則此方法返回 0,如果 dict1 < dict2 和 1 如果 dict1 > dic2,則返回 -1。
示例
下麵的例子展示了 cmp() 方法的用法。
#!/usr/bin/python
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)
當我們運行上麵的程序時,它會產生以下結果 -
Return Value:-1 Return Value:1 Return Value:0
相關用法
- Python dictionary fromkeys()用法及代碼示例
- Python dictionary update()用法及代碼示例
- Python dictionary type()用法及代碼示例
- Python dictionary get()用法及代碼示例
- Python dictionary setdefault()用法及代碼示例
- Python dictionary values()用法及代碼示例
- Python dict()用法及代碼示例
- Python dir()用法及代碼示例
- Python divmod()用法及代碼示例
- Python Wand distort()用法及代碼示例
- Python datetime astimezone()用法及代碼示例
- Python datetime timetuple()用法及代碼示例
- Python datetime timetz()用法及代碼示例
- Python datetime isocalendar()用法及代碼示例
注:本文由純淨天空篩選整理自 Python dictionary cmp() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。