Python:计算列表笛卡尔幂的最短方法

Python:计算列表笛卡尔幂的最短方法,python,list,product,cartesian,Python,List,Product,Cartesian,假设我们有一个列表L。笛卡尔积lxl可以这样计算: product = [(a,b) for a in L for b in L] 笛卡尔幂怎么可能是。。。x L(n次,对于给定的n)可以用一种简单而有效的方法计算?使用: 其中,产品现在是一个iterable;如果要将其具体化为完整列表,请调用列表(产品): >>> from itertools import product >>> list(product(range(3), repeat=2)) [(0

假设我们有一个列表
L
。笛卡尔积
lxl
可以这样计算:

product = [(a,b) for a in L for b in L]
笛卡尔幂怎么可能是。。。x L(n次,对于给定的n)可以用一种简单而有效的方法计算?

使用:

其中,
产品
现在是一个iterable;如果要将其具体化为完整列表,请调用
列表(产品)

>>> from itertools import product
>>> list(product(range(3), repeat=2))
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

为什么这个标记为重复。事实并非如此:OP中只有一个列表,而链接问题有一个列表,这是根本不同的。
>>> from itertools import product
>>> list(product(range(3), repeat=2))
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]