Python 2.7 二维numpy中相同行的分类

Python 2.7 二维numpy中相同行的分类,python-2.7,numpy,classification,Python 2.7,Numpy,Classification,嗨,我想在2D numpy数组中对相同行的索引进行分类。有什么功能可以做吗 大概是这样的: a=[[1,2,3],[2,3,4],[5,6,7],[1,2,3],[1,2,3],[2,3,4] 然后f(a)返回相同的行索引[[0,3,4],[1,5],[2]] 非常感谢您的解决方案这里有一个输出行索引数组列表的方法- def classify_rows(a): sidx = np.lexsort(a.T) b = a[sidx] m = ~(b[1:] == b[:-1]

嗨,我想在2D numpy数组中对相同行的索引进行分类。有什么功能可以做吗

大概是这样的:
a=[[1,2,3],[2,3,4],[5,6,7],[1,2,3],[1,2,3],[2,3,4]
然后
f(a)
返回相同的行索引
[[0,3,4],[1,5],[2]]

非常感谢您的解决方案这里有一个输出行索引数组列表的方法-

def classify_rows(a):
    sidx = np.lexsort(a.T)
    b = a[sidx]
    m = ~(b[1:] == b[:-1]).all(1)
    return np.split(sidx, np.flatnonzero(m)+1)
如果需要列表列表作为输出-

def classify_rows_list(a):
    sidx = np.lexsort(a.T)
    b = a[sidx]
    m = np.concatenate(( [True], ~(b[1:] == b[:-1]).all(1), [True]))
    l = sidx.tolist()
    idx = np.flatnonzero(m)
    return [l[i:j] for i,j in zip(idx[:-1],idx[1:])]
样本运行-

In [78]: a
Out[78]: 
array([[1, 2, 3],
       [2, 3, 4],
       [5, 6, 7],
       [1, 2, 3],
       [1, 2, 3],
       [2, 3, 4]])

In [79]: classify_rows(a)
Out[79]: [array([0, 3, 4]), array([1, 5]), array([2])]

In [80]: classify_rows_list(a)
Out[80]: [[0, 3, 4], [1, 5], [2]]