【问题标题】:is there a binary OR operator in python that works on arrays?python中是否有适用于数组的二元或运算符?
【发布时间】:2015-03-25 09:17:40
【问题描述】:

我从matlab背景到python,我只是想知道python中是否有一个简单的运算符可以执行以下功能:

a = [1, 0, 0, 1, 0, 0]
b = [0, 1, 0, 1, 0, 1]
c = a|b
print c
[1, 1, 0, 1, 0, 1]

或者我是否必须编写一个单独的函数来做到这一点?

【问题讨论】:

    标签: python operators


    【解决方案1】:

    您可以使用列表推导。如果您使用的是 Python 2,请使用 itertools 中的 izip

    c = [x | y for x, y in zip(a, b)]
    

    另外,@georg 在评论中指出您可以导入 按位或 运算符并将其与map 一起使用。这仅比列表理解稍微快一点。 map 在 Python 2 中不需要用 list() 包裹。

    import operator
    c = list(map(operator.or_, a, b))
    

    性能

    列表理解:

    $ python -m timeit -s "a = [1, 0, 0, 1, 0, 0]; b = [0, 1, 0, 1, 0, 1]" \
    > "[x | y for x, y in zip(a, b)]"
    
    1000000 loops, best of 3: 1.41 usec per loop
    

    地图:

    $ python -m timeit -s "a = [1, 0, 0, 1, 0, 0]; b = [0, 1, 0, 1, 0, 1]; \
    > from operator import or_" "list(map(or_, a, b))"
    1000000 loops, best of 3: 1.31 usec per loop
    

    NumPy

    $ python -m timeit -s "import numpy; a = [1, 0, 0, 1, 0, 0]; \
    > b = [0, 1, 0, 1, 0, 1]" "na = numpy.array(a); nb = numpy.array(b); na | nb"
    
    100000 loops, best of 3: 6.07 usec per loop
    

    NumPy(其中ab 已经转换为numpy 数组):

    $ python -m timeit -s "import numpy; a = numpy.array([1, 0, 0, 1, 0, 0]); \
    > b = numpy.array([0, 1, 0, 1, 0, 1])" "a | b"
    
    1000000 loops, best of 3: 1.1 usec per loop
    

    结论:除非你需要 NumPy 进行其他操作,否则不值得转换。

    【讨论】:

      【解决方案2】:

      如果您的数据在 numpy 数组中,那么是的,这将起作用:

      In [42]:
      
      a = np.array([1, 0, 0, 1, 0, 0])
      b = np.array([0, 1, 0, 1, 0, 1])
      c = a|b
      print(c)
      [1 1 0 1 0 1]
      Out[42]:
      [1, 1, 0, 1, 0, 1]
      

      【讨论】:

      • 好的,谢谢。那么它不能在标准数组中工作吗?
      • 不适用于使用您的符号的列表
      • 我个人认为如果你习惯使用来自 matlab 背景的数组,numpy 数组是最合适的数据结构
      • 我可能最终会使用 numpy 数组,但我认为这对于我当前的应用程序来说有点矫枉过正,实际上只是结合了两个决策变量列表
      【解决方案3】:

      map(lambda (a,b): a|b,zip(a,b))

      【讨论】:

      • 当然是最 Python 的方式,我喜欢 map 方法 :) 你知道 map 是否比使用列表理解产生更好的性能吗?
      • 嗯,一个快速但不那么准确的基准! python -m timeit 'a=[0,0,0,1,0]; b=[1,0,1,1,1]; c=map(lambda (a,b): a|b, zip(a,b))':最好的 3:每循环 1.01 微秒和python -m timeit 'a=[0,0,0,1,0]; b=[1,0,1,1,1]; c=[x|y for x,y in zip(a,b)]':最好的 3:每循环 0.646 微秒:)
      • 如果你想要map,只需map(operator.or_, a, b) - 不需要拉链,为你映射拉链。
      【解决方案4】:

      您可以使用numpy.bitwise_or

      >>> import numpy
      >>> a = [1, 0, 0, 1, 0, 0]
      >>> b = [0, 1, 0, 1, 0, 1]
      >>> numpy.bitwise_or(a,b)
      array([1, 1, 0, 1, 0, 1])
      

      【讨论】:

        【解决方案5】:
        c = [q|w for q,w in zip(a,b)]
        print c
        # [1, 1, 0, 1, 0, 1]
        

        【讨论】:

          【解决方案6】:

          我相信它可以用于多个值,因为 zip 返回一个元组

          bitlist = [x & y & z for x, y, z in zip(bitlist, green_mask_h, green_mask_s)]
          

          【讨论】:

            猜你喜欢
            • 2018-09-25
            • 2022-01-24
            • 1970-01-01
            • 1970-01-01
            • 2014-07-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-06-20
            相关资源
            最近更新 更多