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

后端 未结 8 773
别那么骄傲
别那么骄傲 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:46

    and tests whether both expressions are logically True while & (when used with True/False values) tests if both are True.

    In Python, empty built-in objects are typically treated as logically False while non-empty built-ins are logically True. This facilitates the common use case where you want to do something if a list is empty and something else if the list is not. Note that this means that the list [False] is logically True:

    >>> if [False]:
    ...    print 'True'
    ...
    True
    

    So in Example 1, the first list is non-empty and therefore logically True, so the truth value of the and is the same as that of the second list. (In our case, the second list is non-empty and therefore logically True, but identifying that would require an unnecessary step of calculation.)

    For example 2, lists cannot meaningfully be combined in a bitwise fashion because they can contain arbitrary unlike elements. Things that can be combined bitwise include: Trues and Falses, integers.

    NumPy objects, by contrast, support vectorized calculations. That is, they let you perform the same operations on multiple pieces of data.

    Example 3 fails because NumPy arrays (of length > 1) have no truth value as this prevents vector-based logic confusion.

    Example 4 is simply a vectorized bit and operation.

    Bottom Line

    • If you are not dealing with arrays and are not performing math manipulations of integers, you probably want and.

    • If you have vectors of truth values that you wish to combine, use numpy with &.

提交回复
热议问题