Python numpy.where 中的多个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15373753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:56:20  来源:igfitidea点击:

multiple conditions in numpy.where

pythonnumpy

提问by user2162806

With some numpy array a, what I'd like to do is

使用一些 numpy 数组 a,我想做的是

indices = np.where((a < 4) or (a > 12))

This isn't valid. It just returns "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()". But this expression isn'tambiguous, and anyand alldon't do what I want to do. (anyand allcan't take compound expressions either. But ifcan. Confused...)

这是无效的。它只是返回“具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()”。但这种表达是不明确的,any并且all不这样做我想做的事情。(any并且all也不能使用复合表达式。但if可以。困惑......)

回答by YXD

You want to get a logical/boolean array as your argument for where

你想得到一个逻辑/布尔数组作为你的参数 where

You can do x | yor np.logical_or(x,y), where xand yare a < 4and a > 12

你可以做x | yor np.logical_or(x,y), where xand yare a < 4anda > 12