介绍


operator 模块提供了一套与Python的内置运算符对应的高效率函数 . 许多函数名与特殊方法名相同,只是没有双下划线。为了向后兼容性,也保留了许多包含双下划线的函数。函数包含的种类有:对象的比较运算、逻辑运算、数学运算以及序列运算。这个模块提供了很多和Python 一样的操作符, 这里只是封装一个函数进行调用
operator 模块 封装了很多操作相关的函数, 比如 加,减 乘除 ,比较运算 ,逻辑运算 ,矩阵相乘. 还有一些封装好的类, 用起来 效率 比较高效.

常用方法

操作 句法 功能
a+b add(a,b)
串联 seq1 + seq2 concat(seq1,seq2)
a / b truediv(a,b)
整除 a // b floordiv(a,b)
按位与 a & b and_(a,b)
按位异或 a ^ b xor(a,b)
按位取反 ~a invert(a)
按位或 a | b or_(a,b)
取幂 a**b pow(a,b)
取模 a % b mod(a,b)
a * b mul(a,b)
a - b sub(a,b)
小于 a b lt(a,b)
小于等于 a b le(a,b)
等于 a == b eq(a,b)
不等于 a != b ne(a,b)
大于等于 a >= b ge(a,b)
大于 a > b gt(a,b)

三大类


attrgetter

通过attrgetter可以获取对象的属性,然后进行排序操作

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. '''
  4. operator 中 可以使用 attrgetter 获取 对象的 属性
  5. attrgetter('xxx') 来获取对象的属性 .
  6. '''
  7. from operator import attrgetter
  8. class Student:
  9. def __init__(self,name,age,score):
  10. self.name = name
  11. self.age = age
  12. self.score = score
  13. def __str__(self):
  14. return '%s(name=%r,age=%r,score=%r)' % (self.__class__.__name__,self.name,self.age,self.score)
  15. __repr__ = __str__ #重写类的实例输出
  16. if __name__=='__main__':
  17. std1 = Student('A',11,23)
  18. std2 = Student('B',12,24)
  19. std3 = Student('C',23,18)
  20. std4 = Student('D',10,30)
  21. students = [std1,std2,std3,std4]
  22. #按照分数升序排列
  23. new_students_score = sorted(students,key=lambda stu:stu.score,reverse=False)
  24. #按照年龄升序排列
  25. new_students_age = sorted(students,key=attrgetter('age'),reverse=False)
  26. print(new_students_score)
  27. print(new_students_age)
  28. >>>>
  29. [Student(name='C',age=23,score=18), Student(name='A',age=11,score=23), Student(name='B',age=12,score=24), Student(name='D',age=10,score=30)]
  30. [Student(name='D',age=10,score=30), Student(name='A',age=11,score=23), Student(name='B',age=12,score=24), Student(name='C',age=23,score=18)]

itemgetter

这个 类会返回一个可调用对象 ,它会从它的操作数里面取值,会调用操作数的getitem() 方法获取值,如果传入了多个 item , 那么结果就会返回一个元祖

  1. from operator import itemgetter
  2. a = 'abcedfsgs'
  3. itemgetter(1)(a)
  4. >>'b'
  5. itemgetter(1,2,3,4)(a)
  6. >>('b','c','e','d')

进行排序

  1. from operator import itemgetter
  2. def one example():
  3. data = [
  4. ("frank", 10),
  5. ("frank1", 15),
  6. ("frank2", 19),
  7. ("frank3", 12),
  8. ("frank4", 17),
  9. ("frank5", 11),
  10. ("frank6", 18),
  11. ]
  12. data.sort(key=itemgetter(1),reverse=True)
  13. print(data)
  14. >>[('frank2', 19), ('frank6', 18), ('frank4', 17), ('frank1', 15), ('frank3', 12), ('frank5', 11), ('frank', 10)]

methodcaller

你有一个字符串形式的方法名称,想通过它调用某个对象的对应方法

  1. import math
  2. from operator import methodcaller
  3. class Point:
  4. def __init__(self,x,y):
  5. self.x = x
  6. self.y = y
  7. def __str__(self):
  8. return 'Point({0},{1})'.format(self.x,self.y)
  9. def distance(self,x,y):
  10. return math.hypot(self.x-x,self.y-y)
  11. points = [Point(x,y) for x,y in zip([3,2,1,0,0.2],[0.1,-1,5,3,1,8])]
  12. #离原点的距离
  13. sorted(points,key=methodcaller('distance',0,0))
  14. for p in points:
  15. print(p)
  16. >>Point(3,0.1)
  17. >>Point(2,-1)
  18. >>Point(1,5)
  19. >>Point(0,3)
  20. >>Point(0.2,1)