gpt4 book ai didi

python - numpy.nonzero() 与组合逻辑

转载 作者:行者123 更新时间:2023-11-28 22:45:06 32 4
gpt4 key购买 nike

我想将 numpy.nonzero() 与组合逻辑一起使用,但我的尝试最终是这样的:

>>> x
array([[ 3, 5, 4, 2, 2],
[ 2, 5, 3, 100, 4],
[ 3, 5, 4, 100, 3]])
>>> np.nonzero(x > 3 and x < 100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

但不清楚在这种情况下如何应用 any() 或 all() 。我是否必须分成两步然后使用 setdiff()?那会很伤心。

最佳答案

您需要使用运算符 & 来获得您期望的结果:

np.nonzero((x > 3) & (x < 100))

& 做元素方面的“和”

关于python - numpy.nonzero() 与组合逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28916554/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com