根据要求,numpy
解决方案:
import numpy as np
a = np.array([[0,1], [0,2], [0,3], [0,4], [1,5], [1,6], [1,7], [2,8], [2,9]])
_,i = np.unique(a[:,0], return_index=True)
b = np.delete(a, i, axis=0)
(上面被编辑以合并@Jaime的解决方案,这是我为后代着想的原始掩蔽解决方案)
m = np.ones(len(a), dtype=bool)
m[i] = False
b = a[m]
有趣的是,面具似乎更快:
In [225]: def rem_del(a):
.....: _,i = np.unique(a[:,0], return_index=True)
.....: return np.delete(a, i, axis = 0)
.....:
In [226]: def rem_mask(a):
.....: _,i = np.unique(a[:,0], return_index=True)
.....: m = np.ones(len(a), dtype=bool)
.....: m[i] = False
.....: return a[m]
.....:
In [227]: timeit rem_del(a)
10000 loops, best of 3: 181 us per loop
In [228]: timeit rem_mask(a)
10000 loops, best of 3: 59 us per loop