Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图在 Python 中使用 product() 函数。我知道 product() 需要一堆 iterables 并做所有这些的笛卡尔积。
现在我将所有可迭代项放在一个列表中。我想知道如何一次传递该列表中的所有迭代?我不能将列表直接传递给产品,因为产品()会将其视为一个可迭代的。例如,我有一个列表列表:
[[1,2,3],['a','b']]
如何将 [1,2,3] 和 ['a','b'] 传递给产品,使其等效于 product([1,2,3],['a','b'])?
提前致谢!
l = [[1, 2, 3], ('a', 'b')] product(*l)
这称为拆包参数列表。
使用参数扩展(解包):
vals = [[1,2,3],['a','b']] product(*vals)
它的工作原理类似于:
>>> x, y = [[1,2,3],['a','b']] >>> x [1, 2, 3] >>> y ['a', 'b']