NumPy数组 – 逻辑操作

NumPy数组 – 逻辑操作

逻辑运算是用来寻找两个数组或列表或变量之间的逻辑关系。我们可以使用NumPy在两个数据之间进行逻辑运算。下面是我们可以对Numpy数组进行的各种逻辑运算。

AND

numpy模块支持logical_and操作符。它被用来在两个变量之间建立联系。如果两个变量为0,则输出为0;如果两个变量为1,则输出为1;如果一个变量为0,另一个为1,则输出为0。

语法:

numpy.logical_and(var1,var2)

其中,var1和var2是一个单一变量或一个列表/数组。

返回类型:布尔值(真或假)。

示例 1:

这段代码给出了用logical_and操作符进行布尔运算的演示。

# 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))

输出:

NumPy数组 - 逻辑操作

示例 2:

# 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数组 - 逻辑操作

OR

NumPy模块支持logical_or运算符。它也被用来在两个变量之间建立联系。如果两个变量为0,则输出为0;如果两个变量为1,则输出为1;如果一个变量为0,另一个为1,则输出为1。

语法:

numpy.logical_or(var1,var2) 

其中, var1 and var2 are a single variable or a list/array.

返回类型: Boolean value (True or False)

示例:

# 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))

输出:

NumPy数组 - 逻辑操作

NOT

logical_not操作取一个值并将其转换为另一个值。如果值为0,则输出为1,如果值大于或等于1,则输出为0。

语法:

numpy.logical_not(var1) 

其中, var1is a single variable or a list/array.

返回类型: Boolean value (True or False)

示例:

# 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))

输出:

NumPy数组 - 逻辑操作

XOR

logical_xor在两个变量或列表之间进行xor操作。在这个操作中,如果两个值相同,则返回0,否则返回1。

语法:

numpy.logical_xor(var1,var2) 

其中, var1 and var2 are a single variable or a list/array.

返回类型: Boolean value (True or False)

示例:

# 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))

输出:

NumPy数组 - 逻辑操作

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程