python - 如何在 python 中从具有多个条件的数组 A 获取 bool 数组?

标签 python numpy numpy-ndarray

A = np.arange(0,20,1)
A<7

以上代码将返回一个 bool 数组,当 A<7 时其元素为真,否则为假。 我如何获得 x < A < 7 的 bool 数组?

最佳答案

如果你的 x = 3,那么:

a = np.arange(0,20,1)
a
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])

(a>3) & (a<7)
array([False, False, False, False,  True,  True,  True, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False])

如果你想要一个 or 条件,你可以将 & 替换为 |:

(a<3) | (a>7) #Less than 3 or greater than 7
array([ True,  True,  True, False, False, False, False, False,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True])

关于python - 如何在 python 中从具有多个条件的数组 A 获取 bool 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54787096/

相关文章:

python - Numpy sum() 出现 'keepdims' 错误

python - 将 PDF 转换为文本 : remove word breaks

python - Python3 Asyncio 创建任务时出现问题

python - 根据间隙大小更改 numpy 数组中的间隙

python - Python TypeError:需要一个整数(元组类型元组)-(OpenCV/Numpy)

python - 在 1D numpy 数组中的随机位置注入(inject)随机数

python - 将ndarray从float64转换为整数

python - 如何使用Python从支持ftp的网站自动下载文件?

python - 使用Rasterio保存窗口图像(另存为jpg)

python - 如何使用 python/numpy 计算百分位数?