NumPy 数组——逻辑运算
逻辑运算用于查找两个数组或列表或变量之间的逻辑关系。我们可以使用 NumPy 在两个数据之间执行逻辑运算。以下是我们可以对 Numpy 数组执行的各种逻辑操作:
和
numpy 模块支持logical_and运算符。它用于关联两个变量。如果两个变量为 0,则输出为 0,如果两个变量为 1,则输出为 1,如果一个变量为 0,另一个为 1,则输出为 0。
句法:
numpy.logical_and(var1,var2)
Where, var1 and var2 are a single variable or a list/array.
Return type: Boolean value (True or False)
示例 1:
此代码演示了使用logical_and运算符的布尔运算。
Python3
# importing numpy module
import numpy as np
# list 1 represents an array with boolean values
list1 = [True, False, True, False]
# list 2 represents an array with boolean values
list2 = [True, True, False, True]
# logical operations between boolean values
print('Operation between two lists = ',
np.logical_and(list1, list2))
Python3
# importing numpy module
import numpy as np
# list 1 represents an array
# with integer values
list1 = [1, 2, 3, 4, 5, 0]
# list 2 represents an array
# with integer values
list2 = [0, 1, 2, 3, 4, 0]
# logical operations between integer values
print('Operation between two lists:',
np.logical_and(list1, list2))
Python3
# importing numpy module
import numpy as np
# logical operations between boolean values
print('logical_or operation = ',
np.logical_or(True, False))
a = 2
b = 6
print('logical or Operation between two variables = ',
np.logical_or(a, b))
a = 0
b = 0
print('logical or Operation between two variables = ',
np.logical_or(a, b))
# list 1 represents an array with integer values
list1 = [1, 2, 3, 4, 5, 0]
# list 2 represents an array with integer values
list2 = [0, 1, 2, 3, 4, 0]
# logical operations between integer values
print('Operation between two lists = ',
np.logical_or(list1, list2))
Python3
# importing numpy module
import numpy as np
# logical not operations for boolean value
print('logical_not operation = ',
np.logical_not(True))
a = 2
b = 6
print('logical_not Operation = ',
np.logical_not(a))
print('logical_not Operation = ',
np.logical_not(b))
# list 1 represents an array with integer values
list1 = [1, 2, 3, 4, 5, 0]
# logical operations between integer values
print('Operation in list = ',
np.logical_not(list1))
Python3
# importing numpy module
import numpy as np
# logical operations between boolean values
print('Operation between true and true ( 1 and 1) = ',
np.logical_xor(True, True))
print('Operation between true and false ( 1 and 0) = ',
np.logical_xor(True, False))
print('Operation between false and true ( 0 and 1) = ',
np.logical_xor(False, True))
print('Operation between false and false (0 and 0)= ',
np.logical_xor(False, False))
# list 1 represents an array with boolean values
list1 = [True, False, True, False]
# list 2 represents an array with boolean values
list2 = [True, True, False, True]
# logical operations between boolean values
print('Operation between two lists = ',
np.logical_xor(list1, list2))
# list 1 represents an array
# with integer values
list1 = [1, 2, 3, 4, 5, 0]
# list 2 represents an array
# with integer values
list2 = [0, 1, 2, 3, 4, 0]
# logical operations between integer values
print('Operation between two lists = ',
np.logical_xor(list1, list2))
输出:
示例 2:
蟒蛇3
# importing numpy module
import numpy as np
# list 1 represents an array
# with integer values
list1 = [1, 2, 3, 4, 5, 0]
# list 2 represents an array
# with integer values
list2 = [0, 1, 2, 3, 4, 0]
# logical operations between integer values
print('Operation between two lists:',
np.logical_and(list1, list2))
输出:
或者
NumPy 模块支持logical_or运算符。它还用于关联两个变量。如果两个变量为 0,则输出为 0,如果两个变量为 1,则输出为 1,如果一个变量为 0,另一个为 1,则输出为 1。
句法:
numpy.logical_or(var1,var2)
Where, var1 and var2 are a single variable or a list/array.
Return type: Boolean value (True or False)
例子:
蟒蛇3
# importing numpy module
import numpy as np
# logical operations between boolean values
print('logical_or operation = ',
np.logical_or(True, False))
a = 2
b = 6
print('logical or Operation between two variables = ',
np.logical_or(a, b))
a = 0
b = 0
print('logical or Operation between two variables = ',
np.logical_or(a, b))
# list 1 represents an array with integer values
list1 = [1, 2, 3, 4, 5, 0]
# list 2 represents an array with integer values
list2 = [0, 1, 2, 3, 4, 0]
# logical operations between integer values
print('Operation between two lists = ',
np.logical_or(list1, list2))
输出:
不是
logical_not 操作采用一个值并将其转换为另一个值。如果值为 0,则输出为 1,如果值大于或等于 1,则输出为 0。
句法:
numpy.logical_not(var1)
Where, var1is a single variable or a list/array.
Return type: Boolean value (True or False)
例子:
蟒蛇3
# importing numpy module
import numpy as np
# logical not operations for boolean value
print('logical_not operation = ',
np.logical_not(True))
a = 2
b = 6
print('logical_not Operation = ',
np.logical_not(a))
print('logical_not Operation = ',
np.logical_not(b))
# list 1 represents an array with integer values
list1 = [1, 2, 3, 4, 5, 0]
# logical operations between integer values
print('Operation in list = ',
np.logical_not(list1))
输出:
异或
logical_xor 在两个变量或列表之间执行异或运算。在此操作中,如果两个值相同,则返回 0,否则返回 1。
句法:
numpy.logical_xor(var1,var2)
Where, var1 and var2 are a single variable or a list/array.
Return type: Boolean value (True or False)
例子:
蟒蛇3
# importing numpy module
import numpy as np
# logical operations between boolean values
print('Operation between true and true ( 1 and 1) = ',
np.logical_xor(True, True))
print('Operation between true and false ( 1 and 0) = ',
np.logical_xor(True, False))
print('Operation between false and true ( 0 and 1) = ',
np.logical_xor(False, True))
print('Operation between false and false (0 and 0)= ',
np.logical_xor(False, False))
# list 1 represents an array with boolean values
list1 = [True, False, True, False]
# list 2 represents an array with boolean values
list2 = [True, True, False, True]
# logical operations between boolean values
print('Operation between two lists = ',
np.logical_xor(list1, list2))
# list 1 represents an array
# with integer values
list1 = [1, 2, 3, 4, 5, 0]
# list 2 represents an array
# with integer values
list2 = [0, 1, 2, 3, 4, 0]
# logical operations between integer values
print('Operation between two lists = ',
np.logical_xor(list1, list2))
输出: