我有一个数组,例如
Array = [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[1,1,1],[2,2,2]]
我想要输出以下内容的东西:
Repeated = [[1,1,1],[2,2,2]]
保留重复行的数量也可以,例如
Repeated = [[1,1,1],[1,1,1],[2,2,2],[2,2,2]]
我认为解决方案可能包括 numpy.unique,但我无法让它工作,是否有本地 python / numpy 函数?
使用新的axis的功能np.unique https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.unique.html随着return_counts=True这为我们提供了唯一的行以及每行的相应计数,我们可以使用以下命令屏蔽这些行counts > 1从而得到我们想要的输出,就像这样 -
axis
return_counts=True
counts > 1
In [688]: a = np.array([[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[1,1,1],[2,2,2]]) In [689]: unq, count = np.unique(a, axis=0, return_counts=True) In [690]: unq[count>1] Out[690]: array([[1, 1, 1], [2, 2, 2]])