【发布时间】:2023-04-07 17:07:02
【问题描述】:
我有一个二维数组,定义如下:
traces = [['x1',11026,0,0,0,0],
['x0',11087,0,0,0,1],
['x0',11088,0,0,1,3],
['x0',11088,0,0,0,3],
['x0',11088,0,1,0,1]]
我想找到与所选列的多个条件匹配的行的索引。例如我想在这个数组中找到
row[0]=='x0' & row[1]==11088 & row[3]==1 & row[5]=1
按此条件搜索应返回 4。
我尝试使用 numpy.where 但似乎无法使其适用于多种条件
print np.where((traces[:,0] == 'x0') & (traces[:,1] == 11088) & (traces[:,3] == 1) & (traces[:,5] == 1))
以上创建警告
FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison print np.where((traces[:,0] == 'x0') & (traces[:,1] == 11088) & (traces[:,3]
== 1) & (traces[:,5] == 1)) (array([], dtype=int32),)
我也尝试使用numpy.logical_and,但这似乎也不起作用,产生了类似的警告。
我有什么方法可以使用numpy.where 做到这一点,而无需遍历整个二维数组?
谢谢
【问题讨论】:
标签: python python-2.7 numpy