仅当第一列中的值相同时,才根据第二列对numpy数组进行排序

编程入门 行业动态 更新时间:2024-10-12 01:24:19
本文介绍了仅当第一列中的值相同时,才根据第二列对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数组进行排序

本文发布于:2023-10-31 09:19:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545708.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!