'and' (boolean) vs '&' (bitwise) - Why difference in behavior with lists vs numpy arrays?

后端 未结 8 846
别那么骄傲
别那么骄傲 2020-11-22 07:56

What explains the difference in behavior of boolean and bitwise operations on lists vs NumPy arrays?

I\'m confused about the appropriate use of

8条回答
  •  感动是毒
    2020-11-22 08:30

    Good question. Similar to the observation you have about examples 1 and 4 (or should I say 1 & 4 :) ) over logical and bitwise & operators, I experienced on sum operator. The numpy sum and py sum behave differently as well. For example:

    Suppose "mat" is a numpy 5x5 2d array such as:

    array([[ 1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10],
           [11, 12, 13, 14, 15],
           [16, 17, 18, 19, 20],
           [21, 22, 23, 24, 25]])
    

    Then numpy.sum(mat) gives total sum of the entire matrix. Whereas the built-in sum from Python such as sum(mat) totals along the axis only. See below:

    np.sum(mat)  ## --> gives 325
    sum(mat)     ## --> gives array([55, 60, 65, 70, 75])
    

提交回复
热议问题