在大神麥金尼的著作中,對 np.logical_and、np.logical_or、np.logical_xor 三個二元通用函數的說明是:Computer element_wise true value of logical operateion (equivalent to infix operators & , |, ^
代碼體驗示例:
In [1]: import numpy as np
In [2]: a = [0, 1, 2, 3, 0]
In [3]: b = [1, 1, 2, 3, 1]
In [4]: np.logical_and(a, b) # 對每個元素進行 and 運算,返回 bool 值組成的數組
Out[4]: array([False, True, True, True, False], dtype=bool)
In [5]: np.logical_or(a, b) # 對每個元素進行 or 運算,返回 bool 值組成的數組
Out[5]: array([ True, True, True, True, True], dtype=bool)
In [6]: np.logical_xor(a, b) # 對每個元素進行 異或 運算,返回 bool 值組成的數組
Out[6]: array([ True, False, False, False, True], dtype=bool)
說明:
np.logical_and、np.logical_or、np.logical_xor 在執行元素級運算時,語法為 np.logical_and(x1, x2), np.logical_or(x1, x2), np.logical_xor(x1, x2)
x1、 x2 是列表、數組一類的可以轉換成 array 結構的數據容器。
x1、x2 的形狀大小需相同。
x1、x2 的數據類型需是 int、float, bool。
返回的結果是有 True 或 False 組成的形狀和 x1、x2 相同的 Numpy 數組。
