本文介绍了仅当第一列中的值相同时,才根据第二列对numpy数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个numpy数组,其中包含使用OpenCV的findNonZero()方法获得的坐标.我想对它们进行排序以绘制轮廓.
I have a numpy array which contains coordinates got by using findNonZero() method of OpenCV. I want to sort them in order to draw contours.
未排序的numpy数组示例(非坐标):
[[ 0, 2], [ 0, 0], [-1, 8], [-6, 7], [-1, 1]]预期的排序numpy数组:
[[-6, 7], [-1, 1], [-1, 8], [ 0, 0], [ 0, 2]]我希望根据第一列对数组进行排序,如果第一列中的值相等,那么我希望根据第二列对数组进行排序.因此,由于处理是在云上完成的,所以时间复杂度最低吗?
I want the array to be sorted according to the first column and if values in the first column are equal, i want it to be sorted according to the second column. Is there a way to do so with least time complexity since the processing is to be done on cloud?
推荐答案您可以使用 np.lexsort -
You can use np.lexsort -
a[np.lexsort(a[:,::-1].T)]样品运行-
In [42]: a Out[42]: array([[ 0, 2], [ 0, 0], [-1, 8], [-6, 7], [-1, 1]]) In [43]: a[np.lexsort(a[:,::-1].T)] Out[43]: array([[-6, 7], [-1, 1], [-1, 8], [ 0, 0], [ 0, 2]])更多推荐
仅当第一列中的值相同时,才根据第二列对numpy数组进行排序
发布评论