![](/img/trans.png)
[英]How to remove columns in 2D numpy array if one element is smaller or larger than a certain value
[英]Python: numpy array larger and smaller than a value
如何查找范围之间的数字?
c = array[2,3,4,5,6]
>>> c>3
>>> array([False, False, True, True, True]
但是,当我在两个数字之间给出c时,它会返回错误
>>> 2<c<5
>>> ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
欲望输出是
array([False, True, True, False, False]
试试这个,
(c > 2) & (c < 5)
结果
array([False, True, True, False, False], dtype=bool)
Python将2<c<5
评估为(2<c) and (c<5)
这是有效的,除了and
关键字不能像numpy数组那样工作。 (它试图给每个数组转换到一个布尔值,并且这种行为不能被重写,为讨论在这里 )。因此,对于一个矢量and
与numpy的数组操作,你需要这样做:
(2<c) & (c<5)
你可以这样做:
import numpy as np
c = np.array([2,3,4,5,6])
output = [(i and j) for i, j in zip(c>2, c<5)]
输出:
[False, True, True, False, False]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.