Here are two ways:
In [1]: my_array = arange(10)
In [2]: np.where((my_array > 3) & (my_array < 7))
Out[2]: (array([4, 5, 6]),)
In [3]: np.where(logical_and(my_array > 3, my_array < 7))
Out[3]: (array([4, 5, 6]),)
For the first (replacing and with &), be careful to add parentheses appropriately: & has higher precedence than the comparison operators. You can also use *, but I wouldn't recommend it: it's hacky and doesn't make for readable code.
In [4]: np.where((my_array > 3) * (my_array < 7))
Out[4]: (array([4, 5, 6]),)
python技巧|np.where配合区间使用
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- numpy.wherenumpy.where(condition[, x, y])Return elements,...
- 概要 之前在CSDN上写过一个blog(http://blog.csdn.net/wrzcy/article/de...
- 用python也差不多一年多了,python应用最多的场景还是web快速开发、爬虫、自动化运维:写过简单网站、写过...