【Python】operator 模块简单介绍

简单介绍几个常用的函数,其他的请参考文档


operator.concat(a, b)

operator.concat(a, b)

对于 a、b序列,返回 a + b(列表合并)

---------------------------------

operator.countOf(a, b)

返回 b 在 a 中出现的次数


perator.delitem(a, b)

operator.delitem(a, b)

删除 a 中索引为 b 的值


operator.getitem(a, b)

operator.getitem(a, b)

返回 a 中索引为 b 的值


operator.indexOf(a, b)

返回 b 在 a 中首次出现位置的索引值。


operator.setitem(a, b, c)

operator.setitem(a, b, c)

设置 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)

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 相关的信息对应如下:

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)
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)
点滴积累,点滴生活,丰富研究生生活!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值