函数前的Python星号

2024-04-19 18:16:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我遵循本教程:

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))

我想知道在sorted函数调用之前星号的用法。在


Tags: andcomhttpwww教程zipopencvreverse
2条回答

If the syntax *expression appears in the function call, expression must evaluate to an iterable. Elements from this iterable are treated as if they were additional positional arguments; if there are positional arguments x1, ..., xN, and expression evaluates to a sequence y1, ..., yM, this is equivalent to a call with M+N positional arguments x1, ..., xN, y1, ..., yM.

星号是拆包操作员:

>>> 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]

有关拆包操作员的详细信息:

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

相关问题 更多 >