Python3 operator モジュール
Python2.x バージョンでは、cmp()関数を使用して、2つのリスト、数値、文字列などの大小関係を比較します。
Python 3.X バージョンでは、cmp()関数はありません。比較機能を実装する必要がある場合は、導入が必要です。operatorモジュールは、任意のオブジェクトに適しており、含まれるメソッドは次のとおりです。
operator モジュールに含まれるメソッド
operator.le(a, b)
operator.eq(a, b)
operator.ne(a, b)
operator.ge(a, b)
operator.gt(a, b)
operator.__lt__(a, b)
operator.__le__(a, b)
operator.__eq__(a, b)
operator.__ne__(a, b)
operator.__ge__(a, b)
operator.__gt__(a, b)
operator.lt(a, b)とa < b同じ、operator.le(a, b)とa <= b同じ、operator.eq(a, b)とa == b同じ、operator.ne(a, b)とa != b同じ、operator.gt(a, b)とa > b同じ、operator.ge(a, b)とa >= b同じ。
例
import operator
# 数値
x = 10
y = 20
print("x:",x, ", y:",y)
print("operator.lt(x,y): ", operator.lt(x,y))
print("operator.gt(y,x): ", operator.gt(y,x))
print("operator.eq(x,x): ", operator.eq(x,x))
print("operator.ne(y,y): ", operator.ne(y,y))
print("operator.le(x,y): ", operator.le(x,y))
print("operator.ge(y,x): ", operator.ge(y,x))
print()
# 文字列
x = "Google"
y = "サンプル"
print("x:",x, ", y:",y)
print("operator.lt(x,y): ", operator.lt(x,y))
print("operator.gt(y,x): ", operator.gt(y,x))
print("operator.eq(x,x): ", operator.eq(x,x))
print("operator.ne(y,y): ", operator.ne(y,y))
print("operator.le(x,y): ", operator.le(x,y))
print("operator.ge(y,x): ", operator.ge(y,x))
print()
# 戻り値を確認
print("type((operator.lt(x,y)): ", type(operator.lt(x,y)))
上記のコードの出力結果は次のとおりです。
x: 10 , y: 20 operator.lt(x,y): True operator.gt(y,x): True operator.eq(x,x): True operator.ne(y,y): False operator.le(x,y): True operator.ge(y,x): True x: Google , y: サンプル operator.lt(x,y): True operator.gt(y,x): True operator.eq(x,x): True operator.ne(y,y): False operator.le(x,y): True operator.ge(y,x): True
2つのリストを比較:
例
import operator
a = [1, 2]
b = [2, 3]
c = [2, 3]
print("operator.eq(a,b): ", operator.eq(a,b))
print("operator.eq(c,b): ", operator.eq(c,b))
上記のコードの出力結果は次のとおりです。
operator.eq(a,b): False operator.eq(c,b): True
演算子関数
operator モジュールは、Python の組み込み演算子に対応する高効率な関数群を提供します。例えば、operator.add(x, y)は式x+yと同じです。
関数に含まれる種類は、オブジェクトの比較演算、論理演算、数学演算、シーケンス演算です。
オブジェクト比較関数はすべてのオブジェクトに適用され、関数名は対応する比較演算子に基づいて命名されています。
多くの関数名は特別なメソッド名と同じですが、二重アンダースコアがありません。後方互換性のために、二重アンダースコアを含む関数も多数保持されていますが、明確にするために、二重アンダースコアのない関数を使用することをお勧めします。
例
# add(), sub(), mul()
# operator モジュールをインポート
import operator
# 変数を初期化
a = 4
b = 3
# add() を使用して2つの値を加算
print ("add() 演算結果 :",end="");
print (operator.add(a, b))
# sub() を使用して2つの値を減算
print ("sub() 演算結果 :",end="");
print (operator.sub(a, b))
# mul() を使用して2つの値を乗算
print ("mul() 演算結果 :",end="");
print (operator.mul(a, b))
上記のコードの出力結果は次のとおりです。
add() 运算结果 :7 sub() 运算结果 :1 mul() 运算结果 :12
演算 |
構文 |
関数 |
|---|---|---|
加算 |
|
|
文字列連結 |
|
|
帰属テスト |
|
|
除算 |
|
|
除算 |
|
|
ビット単位 AND |
|
|
ビット単位 XOR |
|
|
ビット単位 反転 |
|
|
ビット単位 OR |
|
|
冪乗 |
|
|
同一性 |
|
|
同一性 |
|
|
インデックス代入 |
|
|
インデックス削除 |
|
|
インデックス取得 |
|
|
左シフト |
|
|
剰余 |
|
|
乗算 |
|
|
行列乗算 |
|
|
符号反転(算術) |
|
|
論理否定 |
|
|
単項プラス |
|
|
右シフト |
|
|
スライス代入 |
|
|
スライス削除 |
|
|
スライス取得 |
|
|
文字列フォーマット |
|
|
減算 |
|
|
真値テスト |
|
|
比較 |
|
|
比較 |
|
|
等価 |
|
|
不等価 |
|
|
比較 |
|
|
比較 |
|
|