简体   繁体   English

动态添加到 numpy.where 中的条件

[英]Dynamically add to condition in numpy.where

Depending on the user input, I have different conditions to check for in my array.根据用户输入,我有不同的条件需要在我的数组中检查。 Assume there are a maximum of 3 conditions:假设最多有 3 个条件:

a must be 1 a 必须是 1

b must be positive b 必须是正数

c must be True c 必须为真

Only where these three conditions evaluate as True, the array shall be processed:仅当这三个条件评估为 True 时,才应处理数组:

myArr = np.random.rand(5)
a = np.array([1, 1, 1, 0, 1])
b = np.array([4, 3, -8, 7, 6])
c = np.array([True, False, True, True, True])
valid_indices = np.where((a == 1) & (b > 0) & (c == True))

>> valid_indices
>> Out: (array([0, 4], dtype=int64),)

Not knowing beforehand which of these conditions will be provided, I would have to check like this:事先不知道将提供哪些条件,我将不得不像这样检查:

if a and not b and not c:
    valid_indices = np.where(a == 1)

elif a and not b and c:
    valid_indices = np.where((a == 1) & (c == True))

elif a and b and not c:
    valid_indices = np.where((a == 1) & (b > 0))

elif not a and b and c:
    valid_indices = np.where((b > 0) & (c == True))

elif not a and not b and c:
    valid_indices = np.where(c == True)

elif not a and b and not c:
    valid_indices = np.where((b > 0))

God forbid I add another condition.上帝保佑我添加另一个条件。 Things are getting really messy.事情变得非常混乱。 I am looking for a way to dynamically add to the condition as if it were just a regular string or formatter.我正在寻找一种动态添加到条件的方法,就好像它只是一个常规字符串或格式化程序。 Is that possible?那可能吗?

If you set defaults to the true condition:如果您将默认值设置为 true 条件:

import numpy as np


myArr = np.random.rand(5)

a = np.array([1, 1, 1, 0, 1])
b = np.array([4, 3, -8, 7, 6])
c = np.array([True, False, True, True, True])


def get_id(**kwargs):
    """ a must be 1, b must be positive, c must be True """
    a = kwargs.get("a", np.ones(5))
    b = kwargs.get("b", np.ones(5))
    c = kwargs.get("c", np.ones(5) == 1)
    return np.where((a == 1) & (b > 0) & c)


print(get_id(a=a))
print(get_id(a=a, c=c))
print(get_id(a=a, b=b))
print(get_id(a=a, b=b, c=c))


(array([0, 1, 2, 4]),)
(array([0, 2, 4]),)
(array([0, 1, 4]),)
(array([0, 4]),)

Maybe something like the following can help:也许类似以下内容可以提供帮助:

myArr = np.random.rand(5)
size = myArr.size
all_true = np.repeat(True, size)

a = np.array([1, 1, 1, 0, 1])
b = np.array([4, 3, -8, 7, 6])
c = np.array([True, False, True, True, True])

valid_indices = np.where((a == 1 if 'a' in locals() else all_true) & 
                         (b > 0 if 'b' in locals() else all_true) & 
                         (c == True if 'c' in locals() else all_true))

In this way you can write all conditions and only check if the variable exists in the local varibales by 'a' in locals() .通过这种方式,您可以编写所有条件,并且仅通过'a' in locals()检查变量是否存在于局部变量'a' in locals() If you are defining a , b and c somewhere and refering them in a function you can check if they are defined in the global environment using 'a' in globals() .如果您在某处定义abc并在函数中引用它们,您可以使用'a' in globals()检查它们是否在全局环境中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM