python sort

def cmp(x):
    return x[0][0]


def Sort(contours):
    bbox = [cv2.boundingRect(c) for c in contours]
    (_, ret) = zip(*sorted(zip(bbox, contours), key = cmp))
    return ret

 

zip(a, b):将a和b中的每一项放到一起,即((a[0], b[0]), (a[1], b[1]), (a[2], b[2]), (a[3], b[3])``````)

cmp函数是将要排序的项传入(注意是单个项,不是整个列表),返回当前项中用来比较的元素

 

 

按第一个元素升序,第二个降序:

coor = sorted(coor, key = lambda a : (a[0][0], -a[0][1]))

 

posted @ 2021-09-12 20:37  WTSRUVF  阅读(48)  评论(0编辑  收藏  举报