擅长:python、mysql、java
<p>您可以从<a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.lexsort.html" rel="nofollow">^{<cd1>}</a>输入数组开始,将具有相同的前三个元素的条目依次放入。然后,创建另一个2D数组来存储最后的列条目,这样与每个重复的三元组对应的元素进入相同的行。接下来,找到这个2D数组的<code>max</code>,并为每个这样唯一的三元组得到最终的<code>max</code>输出。下面是实现,假设<code>A</code>作为输入数组-</p>
<pre><code># Lex sort A
sortedA = A[np.lexsort(A[:,:-1].T)]
# Mask of start of unique first three columns from A
start_unqA = np.append(True,~np.all(np.diff(sortedA[:,:-1],axis=0)==0,axis=1))
# Counts of unique first three columns from A
counts = np.bincount(start_unqA.cumsum()-1)
mask = np.arange(counts.max()) < counts[:,None]
# Group A's last column into rows based on uniqueness from first three columns
grpA = np.empty(mask.shape)
grpA.fill(np.nan)
grpA[mask] = sortedA[:,-1]
# Concatenate unique first three columns from A and
# corresponding max values for each such unique triplet
out = np.column_stack((sortedA[start_unqA,:-1],np.nanmax(grpA,axis=1)))
</code></pre>
<p>样本运行-</p>
^{pr2}$