Python 辞書(Dictionary) cmp()メソッド
説明
Python 辞書の cmp() 関数は、2つの辞書の要素を比較するために使用されます。
構文
cmp()メソッドの構文:
cmp(dict1, dict2)
引数
- dict1 -- 比較する辞書。
- dict2 -- 比較する辞書。
戻り値
2つの辞書の要素が同じ場合は0を返し、辞書dict1が辞書dict2より大きい場合は1を返し、辞書dict1が辞書dict2より小さい場合は-1を返します。
例
以下の例は、cmp()関数の使用方法を示しています:
例(Python 2.0+)
#!/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)
上記の例の出力結果は次のとおりです:
Return Value : -1 Return Value : 1 Return Value : 0その他の拡張