Python: shortest way to compute cartesian power of list
本问题已经有最佳答案,请猛点这里访问。
假设我们有一个清单
1 | product = [(a,b) for a in L for b in L] |
笛卡尔乘方
使用
1 | product = itertools.product(L, repeat=n) |
如果
1 2 3 | >>> 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)] |