参考链接: numpy.logical_and
在这里插入图片描述功能:逐元素计算逻辑与运算,详见代码:

实验1:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> np.logical_and(True, False)
False
>>> np.logical_and([True, False], [False, False])
array([False, False])
>>> x = np.arange(5)
>>> np.logical_and(x>1, x<4)
array([False, False,  True,  True, False])
>>> 
>>> x1 = [[True,True],[False, False]]
>>> x2 = [[True, False],[True, False]]
>>> x1
[[True, True], [False, False]]
>>> x2
[[True, False], [True, False]]
>>> np.logical_and(x1, x2)
array([[ True, False],
       [False, False]])
>>> 
>>> 

实验2:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> box = np.arange(35).reshape(5,7)
>>> box
array([[ 0,  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, 26, 27],
       [28, 29, 30, 31, 32, 33, 34]])
>>> # mask = np.logical_and(box[:,3] > 4, box[:,5] < 31)
>>> box[:,3] > 4
array([False,  True,  True,  True,  True])
>>> box[:,5] < 31
array([ True,  True,  True,  True, False])
>>> mask = np.logical_and(box[:,3] > 4, box[:,5] < 31)
>>> mask
array([False,  True,  True,  True, False])
>>> box[mask]
array([[ 7,  8,  9, 10, 11, 12, 13],
       [14, 15, 16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25, 26, 27]])
>>> box[np.logical_and(box[:,3] > 4, box[:,5] < 31)]
array([[ 7,  8,  9, 10, 11, 12, 13],
       [14, 15, 16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25, 26, 27]])
>>> box
array([[ 0,  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, 26, 27],
       [28, 29, 30, 31, 32, 33, 34]])
>>> 
>>> 
>>> 
Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐