zoukankan      html  css  js  c++  java
  • Python模块:operator简单介绍

    Python官方文档地址:https://docs.python.org/3.6/library/operator.html?highlight=operator

    Operator提供的函可用于对象比较,逻辑运算,数学运算和序列运算的类别。

    简单介绍几个常用的函数,其他的可参考官方文档。

    operator.lt(a,b)相当于a < b
    **operator.__lt__(a,b)**
    
    operator.le(a,b )相当于a <= b
    **operator.__le__(a,b)**
    
    operator.eq(a,b)相当于a == b
    **operator.__eq__(a,b)**
    
    operator.ne(a,b)相当于a != b
    **operator.__ne__(a,b)**
    
    operator.ge(a,b)相当于a > b
    **operator.__ge__(a,b)**
    
    operator.gt(a,b)相当于a >= b
    **operator.__gt__(a,b)**
    注:这些函数可以返回任何值,这些值可能会或可能不会被解释为布尔值。
    
    operator.concat(a, b)  对于 a、b序列,返回 a + b(列表合并)
    **operator.__concat__(a, b)**
    
    operator.countOf(a, b) 返回 b 在 a 中出现的次数
    
    perator.delitem(a, b) 删除 a 中索引为 b 的值
    **operator.__delitem__(a, b)**
    
    operator.getitem(a, b) 返回 a 中索引为 b 的值
    **operator.__getitem__(a, b)**
    
    operator.indexOf(a, b) 返回 b 在 a 中首次出现位置的索引值。
    
    operator.setitem(a, b, c) 设置 a 中索引值为 b 的项目值更改为 c
    **operator.__setitem__(a, b, c)**
    
    operator 模块也为属性和项目的查找提供了一些工具。这些工具使得 map(), sorted(), itertools.groupby() 或其他函数 需要的参数的提取更方便更快速。上面的函数有一个共同点,即均接受函数参数。
    operator.attrgetter(attr)
    operator.attrgetter(*attrs)
    返回一个可调用的对象,该对象从运算中获取 'attr' 。如果请求的属性不止一个的话, 返回属性的元组。这些属性的名字可以包括 '.'。
    比如:
    f = attrgetter('name'),调用 f(b) 返回 b.name
    f = attrgetter('name', 'date'), 调用 f(b) 返回 (b.name, b.date)
    f = attrgetter('name.first', 'name.last'), 调用 f(b) 返回 (b.name.first, b.name.last)
    相当于:
    
    def attrgetter(*items):
        if any(not isinstance(item, str) for item in items):
            raise TypeError('attribute name must be a string')
        if len(items) == 1:
            attr = items[0]
            def g(obj):
                return resolve_attr(obj, attr)
        else:
            def g(obj):
                return tuple(resolve_attr(obj, attr) for attr in items)
        return g
    
    def resolve_attr(obj, attr):
        for name in attr.split("."):
            obj = getattr(obj, name)
        return obj
    
    operator.itemgetter(item) operator.itemgetter(*items) 返回一个可调用的对象,该对象通过运算符的 __getitem__()的方法 从运算中获取 item 。如果指定了多个 item , 返回查找值的元组。 比如: f = itemgetter(2), 调用 f(r) 返回 r[2] g = itemgetter(2, 5, 3), 调用 f(r) 返回 (r[2], r[3], r[3]) 相当于
    def itemgetter(*items):
        if len(items) == 1:
            item = items[0]
            def g(obj):
                return obj[item]
        else:
            def g(obj):
                return tuple(obj[item] for item in items)
    return g
    运算符的 __getitem__()方法可接受任意类型的项目。字典接收任意的哈希值。列表、元组和字符串接收一个索引或字符片段。 >>> itemgetter(1)('ABCDEFG') 'B' >>> itemgetter(1,3,5)('ABCDEFG') ('B', 'D', 'F') >>> itemgetter(slice(2,None))('ABCDEFG') 'CDEFG' 使用 itemgetter() 从元组序列中获取指定的域值,比如: >>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] >>> getcount = itemgetter(1) >>> map(getcount, inventory) [3, 2, 5, 1] >>> sorted(inventory, key=getcount) [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]

    最后 operator 相关的信息对应如下

    此表显示抽象操作如何与Python语法中的运算符符号以及operator模块中的函数相对应

    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 truediv(a, b)
    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)
    Matrix Multiplication a @ b matmul(a, b)
    Negation (Arithmetic) - a neg(a)
    Negation (Logical) not a not_(a)
    Positive + a pos(a)
    Right Shift a >> b rshift(a, b)
    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)
  • 相关阅读:
    UVa532 Dungeon Master 三维迷宫
    6.4.2 走迷宫
    UVA 439 Knight Moves
    UVa784 Maze Exploration
    UVa657 The die is cast
    UVa572 Oil Deposits DFS求连通块
    UVa10562 Undraw the Trees
    UVa839 Not so Mobile
    327
    UVa699 The Falling Leaves
  • 原文地址:https://www.cnblogs.com/pinpin/p/10168144.html
Copyright © 2011-2022 走看看