为什么 sorted(contour)[0] 不同于 sorted(contour)[:1]

why is sorted(contour)[0] different from sorted(contour)[:1]

我试图在包含 2 个轮廓的图像中找到最大的轮廓,其中一个不是近线。轮廓线是从图像的边缘切割的。另一个很近,包含一个区域。

roi_cnts = sorted(roi_cnts, key = cv2.contourArea, reverse = True)[:1]

不同于

roi_cnts = max(roi_cnts, key=cv2.contourArea)
#or
roi_cnts = sorted(roi_cnts, key = cv2.contourArea, reverse = True)[0]

我不明白为什么这两种方法有不同的结果。

切片符号 [:1] 生成包含第一个元素的 1 列表。关键符号 [0] 产生第一个元素,但不在列表中。

>>> mylist = range (10)
>>> mylist[:1]
[0]
>>> mylist[0]
0