我说的是这个模块:http: //docs.python.org/library/operator.html
来自文章:
运算符模块导出一组在C中实现的函数,这些函数对应于Python的内部运算符.例如,operator.add(x,y)等价于表达式x + y.函数名称是用于特殊类方法的函数名称; 为方便起见,还提供了没有前导和尾随__的变体.
我不确定我理解这个模块的好处或目的.
Joh*_*uhy 38
可能最流行的用法是operator.itemgetter.给定lst元组列表,您可以按第i个元素排序:lst.sort(key=operator.itemgetter(i))
当然,你可以通过定义自己的键功能在没有操作符的情况下做同样的事情,但操作员模块使它稍微整洁.
至于其余部分,python允许一种功能性的编程风格,因此它可以出现 - 例如,Greg的简化示例.
你可能会争辩说:"为什么operator.add我只能这样做add = lambda x, y: x+y?" 答案是:
operator.add 是(我认为)稍快.Gre*_*ill 26
一个例子是使用该reduce()功能:
>>> import operator
>>> a = [2, 3, 4, 5]
>>> reduce(lambda x, y: x + y, a)
14
>>> reduce(operator.add, a)
14
Run Code Online (Sandbox Code Playgroud)
例如获取成员为元组的列表中的列,按列排序:
def item_ope():
s = ['h', 'e', 'l', 'l', 'o']
print operator.getitem(s, 1)
# e
print operator.itemgetter(1, 4)(s)
# ('e', 'o')
inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
get_count = operator.itemgetter(1)
print map(get_count, inventory)
# [3, 2, 5, 1]
print sorted(inventory, key=get_count)
# [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]
Run Code Online (Sandbox Code Playgroud)
看一个更实际的例子,我们想按键或值对字典进行排序:
def dict_sort_by_value():
dic_num = {'first': 11, 'second': 2, 'third': 33, 'Fourth': 4}
# print all the keys
print dic_num.keys()
# ['second', 'Fourth', 'third', 'first']
# sorted by value
sorted_val = sorted(dic_num.items(), key=operator.itemgetter(1))
# [('second', 2), ('Fourth', 4), ('first', 11), ('third', 33)]
print sorted_val
# sorted by key
sorted_key = sorted(dic_num.items(), key=operator.itemgetter(0))
print sorted_key
# [('Fourth', 4), ('first', 11), ('second', 2), ('third', 33)]
Run Code Online (Sandbox Code Playgroud)
另一个示例,当我们想要获取最大值及其在列表中的索引时:
def get_max_val_idx():
lst = [1, 7, 3, 5, 6]
max_val = max(lst)
print max_val
# 7
max_idx = lst.index(max_val)
print max_idx
# 1
# simplify it by use operator
index, value = max(enumerate(lst), key=operator.itemgetter(1))
print index, value
# 1 7
Run Code Online (Sandbox Code Playgroud)
更多演示如下:
import operator
def cmp_fun():
a, b = 5, 3
print operator.le(a, b)
# False
print operator.gt(a, b)
# True
def lst_ope():
lst = [1, 2, 3]
print operator.indexOf(lst, 2)
# 1
lst1 = [1, 2, 3, 2]
print operator.countOf(lst1, 2)
# 2
def cal_ope():
lst1 = [0, 1, 2, 3]
lst2 = [10, 20, 30, 40]
print map(operator.mul, lst1, lst2)
# [0, 20, 60, 120]
print sum(map(operator.mul, lst1, lst2))
# 200
a, b = 1, 3
print operator.iadd(a, b)
# 4
Run Code Online (Sandbox Code Playgroud)
从python 文档中查看更多信息
当您需要将函数作为参数传递给某物时,该模块很有用。然后有两个选项:使用operator模块,或定义一个新函数(使用def或lambda)。如果你动态定义一个函数,如果你需要pickle这个函数,要么将它保存到磁盘,要么在进程之间传递它,这可能会产生问题。虽然itemgetter是可腌制的,但动态定义的函数(withdef或lambda)不是。在以下示例中,替换itemgetter与lambda表达将导致PicklingError。
from operator import itemgetter
def sort_by_key(sequence, key):
return sorted(sequence, key=key)
if __name__ == "__main__":
from multiprocessing import Pool
items = [([(1,2),(4,1)], itemgetter(1)),
([(5,3),(2,7)], itemgetter(0))]
with Pool(5) as p:
result = p.starmap(sort_by_key, items)
print(result)
Run Code Online (Sandbox Code Playgroud)