我有两个数组。让我们说它们看起来像这样:
time1 = [ 1 2 3 ] and time2 = [ 2 4 6]
[ 4 5 6 ]
[ 7 8 9 ]
我想只选择time1中第一列在time2范围内的行。例如,从这个数据集中,我会绘制[4 5 6]行,因为4在2-6的范围内。我试图从数组time1中选择行,如下所示:
selectedtimes = time1(any(time1[:,0] < time2[-1]) and any(time1[:,0] > time2[0]))
我目前正在接收不可调用的对象错误(如下所示),并且我很困惑。有没有更好的方法来重写这一行?
'numpy.ndarray' object is not callable
帮助表示赞赏!
答案 0 :(得分:3)
您可以在此处使用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]])
答案 1 :(得分:0)
使用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)
>>>