# 从NumPy 2D数组中删除重复的列和行

``coordskeys = np.array(list(set([tuple(x) for x in coordskeys])))` `

` `>>> a = np.array([[1, 1], [2, 3], [1, 1], [5, 4], [2, 3]]) >>> unique_rows(a) array([[1, 1], [2, 3],[5, 4]])` `

` `import numpy as np def unique(a): a = np.sort(a) b = np.diff(a) b = np.r_[1, b] return a[b != 0]` `

` `import numpy as np def unique(a): order = np.lexsort(aT) a = a[order] diff = np.diff(a, axis=0) ui = np.ones(len(a), 'bool') ui[1:] = (diff != 0).any(axis=1) return a[ui]` `

` `def unique_rows(a): a = np.ascontiguousarray(a) unique_a = np.unique(a.view([('', a.dtype)]*a.shape[1])) return unique_a.view(a.dtype).reshape((unique_a.shape[0], a.shape[1]))` `

` `>>> a = np.array([[1, 1], [2, 3], [1, 1], [5, 4], [2, 3]]) >>> unique_rows(a) array([[1, 1], [2, 3], [5, 4]])` `

` `import numpy as np def unique2d(a): x, y = aT b = x + y*1.0j idx = np.unique(b,return_index=True)[1] return a[idx]` `

` `a = np.array([[1, 1], [2, 3], [1, 1], [5, 4], [2, 3]]) unique2d(a) array([[1, 1], [2, 3], [5, 4]])` `
` `>>> import numpy as NP >>> # create a 2D NumPy array with some duplicate rows >>> A array([[1, 1, 1, 5, 7], [5, 4, 5, 4, 7], [7, 9, 4, 7, 8], [5, 4, 5, 4, 7], [1, 1, 1, 5, 7], [5, 4, 5, 4, 7], [7, 9, 4, 7, 8], [5, 4, 5, 4, 7], [7, 9, 4, 7, 8]]) >>> # first, sort the 2D NumPy array row-wise so dups will be contiguous >>> # and rows are preserved >>> a, b, c, d, e = AT # create the keys for to pass to lexsort >>> ndx = NP.lexsort((a, b, c, d, e)) >>> ndx array([1, 3, 5, 7, 0, 4, 2, 6, 8]) >>> A = A[ndx,] >>> # now diff by row >>> A1 = NP.diff(A, axis=0) >>> A1 array([[0, 0, 0, 0, 0], [4, 3, 3, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 1, 0, 0], [2, 5, 0, 2, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]) >>> # the index array holding the location of each duplicate row >>> ndx = NP.any(A1, axis=1) >>> ndx array([False, True, False, True, True, True, False, False], dtype=bool) >>> # retrieve the duplicate rows: >>> A[1:,:][ndx,] array([[7, 9, 4, 7, 8], [1, 1, 1, 5, 7], [5, 4, 5, 4, 7], [7, 9, 4, 7, 8]])` `

numpy_indexed包（免责声明：我是它的作者）将user545424发布的解决scheme包装在一个很好的testing界面中，加上许多相关的function：

` `import numpy_indexed as npi npi.unique(coordskeys)` `

` `>>> x = [(1, 1), (2, 3), (1, 1), (5, 4), (2, 3)] >>> y = list(set(x)) >>> y [(5, 4), (2, 3), (1, 1)] >>>` `