如何有效检查numpy数组包含给定范围内的项目? [英] How to check efficiently numpy array contains item within given range?

查看:181
本文介绍了如何有效检查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()

在您的特定情况下.基本上,您应该结合两个基于元素的操作.寻找逻辑功能以了解更多详细信息

I have a numpy array, called a , I want to check whether it contains an item in a range, specified by two values.

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

mintrshold=33
maxtreshold=66

My solution:

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

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

Can you suggest me a more effective, pythonic way?

解决方案

Numpy arrays doesn't work well with pythonic a < x < b. But there's func for this:

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

or

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

in your particular case. Basically, you should combine two element-wise ops. Look for logic funcs for more details

这篇关于如何有效检查numpy数组包含给定范围内的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆