我有一个数组,例如
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 函数?
最佳答案
使用 np.unique
的新 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]])
关于python - 你如何在 numpy 数组中查找和保存重复的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48099479/