这是一种方法 -
# Lex-sort combining cols-1,3 with col-3 setting the primary order
sidx = np.lexsort(a[:,[1,3]].T)
# Indices at intervals change for column-3. These would essentially
# tell us the last indices for each group in a lex-sorted array
idx = np.append(np.flatnonzero(a[1:,3] > a[:-1,3]), a.shape[0]-1)
# Finally, index into idx with lex-sorted indices to give us
# the last indices in a lex-sorted version, which is equivalent
# of picking up the highest of each group
out = a[sidx[idx]]
样本运行 -
In [234]: a # Input array
Out[234]:
array([[ 25, 29, 19, 93],
[ 27, 59, 14, 93],
[ 24, 46, 15, 93],
[ 79, 87, 50, 139],
[ 13, 86, 32, 139],
[ 56, 25, 85, 142],
[ 62, 62, 68, 142],
[ 27, 25, 20, 150],
[ 29, 53, 71, 150],
[ 64, 67, 21, 150],
[ 96, 57, 73, 150]])
In [235]: out # Output array
Out[235]:
array([[ 27, 59, 14, 93],
[ 79, 87, 50, 139],
[ 62, 62, 68, 142],
[ 64, 67, 21, 150]])
通过视图提升性能
我们可以用切片a[:,1::2]
代替a[:,[1,3]]
使用相同的内存空间,从而有望带来性能提升。
让我们验证一下内存视图 -
In [240]: np.may_share_memory(a,a[:,[1,3]])
Out[240]: False
In [241]: np.may_share_memory(a,a[:,1::2])
Out[241]: True