python - 如何有效地检查 numpy 数组包含给定范围内的项目?

标签 python arrays numpy

我有一个名为 a 的 numpy 数组,我想检查它是否包含一个范围内的项目,该范围由两个值指定。

import numpy as np
a = np.arange(100)

mintrshold=33
maxtreshold=66

我的解决方案:

goodItems = np.zeros_like(a)
goodItems[(a<maxtreshold) & (a>mintrshold)] = 1

if goodItems.any(): 
   print (there s an item within range)

你能建议我一个更有效的 pythonic 方法吗?

最佳答案

Numpy 数组不适用于 pythonic a < x < b .但是有一个功能:

np.logical_and(a > mintrshold, a < maxtreshold)

np.logical_and(a > mintrshold, a < maxtreshold).any()

在您的特定情况下。基本上,您应该结合两个元素方面的操作。寻找logic funcs了解更多详情

关于python - 如何有效地检查 numpy 数组包含给定范围内的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44281968/

相关文章:

python - 使用 Pandas Align 时,时间序列数据帧返回错误 - valueError : cannot join with no overlapping index names

javascript - 如何对对象数组中的多个属性值进行计数和分组?

python - 重复和选择函数的轻微变化的快速 Numpy 计算

python - paramiko:打开和返回 sftp 连接的方法

python - 从 Beam Python 示例中了解 "|"和 ">>"

php - while循环有问题吗?

python - 根据给定索引交换两个列表的值

numpy - 对非常大的一维数组进行排序

python - Web Scraping w/BeautifulSoup4 - 如何过滤包含特定字符串的标签?

arrays - 在 VHDL 中初始化数组 : How exactly does it work?