Python 函数前的星号

3 python opencv python-2.7 python-3.x

我正在关注这个教程:

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

其中一行中有一个函数:

(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
        key=lambda b:b[1][i], reverse=reverse))
Run Code Online (Sandbox Code Playgroud)

我想知道函数调用之前的星号有什么用sorted

小智 9

Asterisk 是解包运算符:

>>> list(range(3, 6))            # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> list(range(*args))            # call with arguments unpacked from a list
[3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

有关拆包运算符的更多信息:

https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists