如何从numpy中选择行

如何从numpy中选择行,numpy,Numpy,我有一个numpy数组nList和一个标签列表tList,它保留了nList的顺序,并记录了nList中每个元素所属的类。例如,对于两个不同的类0和1 nList = [11 22 33 44 55] tList = [0 0 1 0 1] 这意味着11,22,44属于一起,33,55也属于一起 是否有一种pythonic方法可以按类从nList中选择元素 我试过了 for clusterId in np.unique(cls): indices = [i for i in range(len

我有一个numpy数组nList和一个标签列表tList,它保留了nList的顺序,并记录了nList中每个元素所属的类。例如,对于两个不同的类0和1

nList = [11 22 33 44 55]
tList = [0 0 1 0 1]
这意味着11,22,44属于一起,33,55也属于一起

是否有一种pythonic方法可以按类从nList中选择元素

我试过了

for clusterId in np.unique(cls):
indices = [i for i in range(len(cls)) if cls[i]==clusterId]
print 'Class ', clusterId
for idx in indices:
    print '\t', x[idx,].tolist()
还有更好的办法吗?

IIUC:

#selecting class 0
>>> nList[tList == 0]
array([11, 22, 44])

#selecting class 1
>>> nList[tList == 1]
array([33, 55])