【发布时间】:2020-12-13 09:46:43
【问题描述】:
我有以下清单:
L = [0, 25, 50, 75, 100]
我想查找此列表的所有可能组合,但一次查找两个元素,例如:
Combi = [(0, 0), (0, 25), (25,0), (25, 25), (0, 0), (0, 50), (50, 0), (50, 50), (0, 0), (0, 75), (75, 0), (75, 75)...]
等等。
有没有简洁的方法来实现这一点?
【问题讨论】:
-
documentation 对此进行了介绍。使用参数
repeat=2 -
list(combinations_with_replacement(L, 2))? -
@Prune 给了我
(0, 0), (0, 25), (0, 50)...等组合。我想要我提到的每对重复组合的结果。 -
@deadshot 也没有给我每对的重复组合。还有什么我可以尝试的吗?
-
list(combinations_with_replacement(L, 2)) + list(combinations_with_replacement(L[::-1], 2))?
标签: python python-3.x combinations itertools cartesian-product