[英]Selecting rows from python array
I have two arrays. 我有两个数组。 Let's say they look like this:
让我们说它们看起来像这样:
time1 = [ 1 2 3 ] and time2 = [ 2 4 6]
[ 4 5 6 ]
[ 7 8 9 ]
I would like to select only the rows from time1 for which the first column is within the range of time2. 我想只选择time1中第一列在time2范围内的行。 For example, from this data set, I would plot the [4 5 6] row, because 4 is in the range of 2 - 6. I am trying to select the rows from array time1 like this:
例如,从这个数据集中,我会绘制[4 5 6]行,因为4在2-6的范围内。我试图从数组time1中选择行,如下所示:
selectedtimes = time1(any(time1[:,0] < time2[-1]) and any(time1[:,0] > time2[0]))
I am currently receiving the object not callable error (shown below), and am quite stuck. 我目前收到的对象不可调用错误(如下所示),我很困惑。 Is there a better way to rewrite this line?
有没有更好的方法来重写这一行?
'numpy.ndarray' object is not callable
Help appreciated! 帮助赞赏!
You can use numpy.logical_and
here: 你可以在这里使用
numpy.logical_and
:
>>> np.logical_and(time1[:,0] > time2[0], time1[:,0] < time2[-1] )
array([False, True, False], dtype=bool)
>>> time1[np.logical_and(time1[:,0] > time2[0], time1[:,0] < time2[-1] )]
array([[4, 5, 6]])
Using for
and if
: 使用
for
和if
:
>>> time1 = ((1,2,3),(4,5,6),(7,8,9))
>>> time2 = (2,4,6)
>>> for x in time1:
... if x[0] in time2:
... print x
...
(4, 5, 6)
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.