带numpy数组的布尔表达式[复制]

时间:2021-11-21 12:13:14

This question already has an answer here:

这个问题已经有了答案:

Basically i am trying out boolean expressions with numpy arrays , for example something like this:

基本上,我正在尝试使用numpy数组的布尔表达式,例如:

import numpy as np
a = np.array([1,0,1])
b = np.array([0,1,1])
c = np.array([1,0,1])

if (a ==b) or (a==c):
    d = [2,5,5]
else : d = [1,5,5]
print d

This returns an error

这将返回一个错误

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

ValueError:具有多个元素的数组的真值是不明确的。使用a.any()或所有()

What would be the correct way to code this?

编码这个的正确方法是什么?

1 个解决方案

#1


0  

the condition should read:

条件应该读:

if (a==b).all() or (a==c).all():

see this question

看到这个问题

#1


0  

the condition should read:

条件应该读:

if (a==b).all() or (a==c).all():

see this question

看到这个问题