Python 使用 a.any() 或 a.all()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34472814/
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-19 15:02:46  来源:igfitidea点击:

Use a.any() or a.all()

pythonnumpy

提问by Moe Steen

x = np.arange(0,2,0.5)
valeur = 2*x

if valeur <= 0.6:
    print ("this works")
else:   
    print ("valeur is too high")

here is the error I get:

这是我得到的错误:

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

I have read several posts about a.any() or a.all() but still can't find a way that really clearly explain how to fix the problem. I see why Python does not like what I wrote but I am not sure how to fix it.

我已经阅读了几篇关于 a.any() 或 a.all() 的帖子,但仍然找不到真正清楚地解释如何解决问题的方法。我明白为什么 Python 不喜欢我写的东西,但我不知道如何修复它。

回答by poke

If you take a look at the result of valeur <= 0.6, you can see what's causing this ambiguity:

如果您查看 的结果valeur <= 0.6,您可以看到造成这种歧义的原因:

>>> valeur <= 0.6
array([ True, False, False, False], dtype=bool)

So the result is another array that has in this case 4 boolean values. Now what should the result be? Should the condition be true when one value is true? Should the condition be true only when all values are true?

所以结果是另一个数组,在这种情况下有 4 个布尔值。现在结果应该是什么?当一个值为真时,条件是否应该为真?只有当所有值都为真时,条件才应该为真吗?

That's exactly what numpy.anyand numpy.alldo. The former requires at least one true value, the latter requires that all values are true:

这正是numpy.anynumpy.all做的。前者要求至少有一个真值,后者要求所有值都为真:

>>> np.any(valeur <= 0.6)
True
>>> np.all(valeur <= 0.6)
False

回答by hpaulj

You comment:

你评论:

valeur is a vector equal to [ 0. 1. 2. 3.] I am interested in each single term. For the part below 0.6, then return "this works"....

valeur 是一个等于 [ 0. 1. 2. 3.] 的向量,我对每一项都感兴趣。对于低于 0.6 的部分,则返回“this works”....

If you are interested in each term, then write the code so it deals with each. For example.

如果您对每个术语都感兴趣,那么编写代码以便处理每个术语。例如。

for b in valeur<=0.6:
    if b:
        print ("this works")
    else:   
        print ("valeur is too high")

This will write 2 lines.

这将写入 2 行。

The error is produced by numpycode when you try to use it a context that expects a single, scalar, value. if b:...can only do one thing. It does not, by itself, iterate through the elements of bdoing a different thing for each.

numpy当您尝试在需要单个标量值的上下文中使用它时,代码会产生该错误。 if b:...只能做一件事。它本身不会遍历b为每个元素做不同事情的元素。

You could also cast that iteration as list comprehension, e.g.

您还可以将该迭代转换为列表理解,例如

['yes' if b else 'no' for b in np.array([True, False, True])]

回答by Gursel Karacor

This should also work and is a closer answer to what is asked in the question:

这也应该有效,并且是对问题中所问内容的更接近答案:

for i in range(len(x)):
    if valeur.item(i) <= 0.6:
        print ("this works")
    else:   
        print ("valeur is too high")