Pulds1918 Pulds1918 - 2 months ago 6
Python Question

What is the use of lambdas in the zip function

I have seen both

zip
and
lamdba
separately but I don't really understand this code:

zip(*sorted(zip(cnts, boundingBoxes),
key=lambda b:b[1][i], reverse=reverse))


It is taken from:
http://www.pyimagesearch.com/2015/04/20/sorting-contours-using-python-and-opencv/#comment-405768

Answer

The code is used for custom sorting of the list. The zip of the contours and boundingBoxes gives a list where the first 2 values represent the x and y coordinates respectively. Hence, by changing i to be 0 or 1, the list can be sorted by the x or y coordinates, which is the purpose of the tutorial.

Comments