首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在python中应该在什么情况下使用内置的'operator‘模块?

在python中应该在什么情况下使用内置的'operator‘模块?
EN

Stack Overflow用户
提问于 2009-01-22 03:03:47
回答 6查看 21.2K关注 0票数 52

我说的是这个模块:http://docs.python.org/library/operator.html

摘自文章:

运算符模块导出一组用C实现的函数,这些函数对应于Python的内部运算符。例如,operator.add(x,y)等同于表达式x+y。函数名是用于特殊类方法的名称;为方便起见,还提供了没有前导和尾随__的变体。

我不确定我是否理解了这个模块的好处或目的。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2009-01-22 03:19:41

最流行的用法可能是operator.itemgetter。给定一个元组列表lst,您可以通过以下方式按第i个元素进行排序:lst.sort(key=operator.itemgetter(i))

当然,您也可以通过定义自己的键函数来完成没有operator的相同操作,但operator模块使其更简洁一些。

至于其余部分,python允许使用函数式编程风格,因此可以使用它--例如,Greg的reduce示例。

你可能会说:“当我可以做add = lambda x, y: x+y的时候,为什么我还需要operator.add呢?”答案是:

  1. operator.add (我认为)稍微快一点。
  2. 它使代码更容易理解,对于你,或者其他人以后看它。他们不需要寻找add的定义,因为他们知道操作符模块does.
  3. operator.add是可挑取的,而lambda是不可拾取的。这意味着可以将函数保存到磁盘或在进程之间传递。
票数 45
EN

Stack Overflow用户

发布于 2009-01-22 03:15:07

一个例子是使用reduce()函数:

代码语言:javascript
复制
>>> import operator
>>> a = [2, 3, 4, 5]
>>> reduce(lambda x, y: x + y, a)
14
>>> reduce(operator.add, a)
14
票数 33
EN

Stack Overflow用户

发布于 2017-11-16 10:53:06

例如,获取列表中成员为元组的列,按列排序:

代码语言:javascript
复制
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)]

看一个更实际的例子,我们想要按键或值对字典进行排序:

代码语言:javascript
复制
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)]

另一个例子,当我们想要得到最大值和它在列表中的索引时:

代码语言:javascript
复制
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

更多的demo如下:

代码语言:javascript
复制
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

python doc了解更多信息

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/467920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档