python operator模組講解
阿新 • • 發佈:2018-12-05
這些函式屬於執行物件比較,邏輯運算,數學運算,序列運算和抽象型別測試的類別。
operator.lt(a, b) #等價於a<b
operator.le(a, b) #等價於a<=b
operator.eq(a, b) #等價於a==b
operator.ne(a, b) <span style="font-family: Arial, Helvetica, sans-serif;">#等價於a!=b</span>
operator.ge(a, b) <span style="white-space:pre"> </span> #等價於a>=b
operator.gt(a, b) <span style="white-space:pre"> </span> #等價於a>b
operator.concat(a, b) #對於 a、b序列,返回 a + b
operator.countOf(a, b) #返回 b 在 a 中出現的次數
perator.delitem(a, b) #刪除 a 中索引為 b 的值
operator.getitem(a, b) #返回 a 中索引為 b 的值
operator.indexOf(a, b) #返回 b 在 a 中首次出現位置的索引值。
operator.setitem(a, b, c) #設定 a 中索引值為 b 的專案值更改為 c
operator.attrgetter(attr) #返回一個可呼叫的物件,該物件從運算中獲取 'attr' 。如果請求的屬性不止一個的話, 返回屬性的元組。這些屬性的名字可以包括 '.'。
Operation | Syntax | Function |
---|---|---|
Addition | a + b | add(a, b) |
Concatenation | seq1 + seq2 | concat(seq1, seq2) |
Containment Test | obj in seq | contains(seq, obj) |
Division | a / b | div(a, b) (without future |
Division | a / b | truediv(a, b) (with future.division) |
Division | a // b | floordiv(a, b) |
Bitwise And | a & b | and_(a, b) |
Bitwise Exclusive Or | a ^ b | xor(a, b) |
Bitwise Inversion | ~ a | invert(a) |
Bitwise Or | a b | or_(a, b) |
Exponentiation | a ** b | pow(a, b) |
Identity | a is b | is_(a, b) |
Identity | a is not b | is_not(a, b) |
Indexed Assignment | obj[k] = v | setitem(obj, k, v) |
Indexed Deletion | del obj[k] | delitem(obj, k) |
Indexing | obj[k] | getitem(obj, k) |
Left Shift | a << b | lshift(a, b) |
Modulo | a % b | mod(a, b) |
Multiplication | a * b | mul(a, b) |
Negation (Arithmetic) | - a | neg(a) |
Negation (Logical) | not a | not_(a) |
Positive | + a | pos(a) |
Right Shift | a >> b | rshift(a, b) |
Sequence Repetition | seq * i | repeat(seq, i) |
Slice Assignment | seq[i:j] = values | setitem(seq, slice(i, j), values) |
Slice Deletion | del seq[i:j] | delitem(seq, slice(i, j)) |
Slicing | seq[i:j] | getitem(seq, slice(i, j)) |
String Formatting | s % obj | mod(s, obj) |
Subtraction | a - b | sub(a, b) |
Truth Test | obj | truth(obj) |
Ordering | a < b | lt(a, b) |
Ordering | a <= b | le(a, b) |
Equality | a == b | eq(a, b) |
Difference | a != b | ne(a, b) |
Ordering | a >= b | ge(a, b) |
Ordering | a > b | gt(a, b) |