如何通过从列表中添加两个元素来获得可能的元素组合
How to get possible combinations of elements by adding two elements from list
如果列表是 [4,3,2,5]
因此对将是 (43, 25), (3, 245), (53, 24)
因为它们是列表中每个元素的成对组合
我的问题是,如何获得这些对的一般列表的上述对?
对于原始列表的每个排列,取每个分区。这看起来像:
from itertools import permutations
l = [4,3,2,5]
def combos(l):
for p in permutations(l):
p = "".join(map(str, p))
yield from ((int(p[:n]), int(p[n:])) for n in range(1, len(p)))
list(combos(l))
这导致:
[(4, 325),
(43, 25),
(432, 5),
(4, 352),
(43, 52),
(435, 2),
(4, 235),
(42, 35),
(423, 5),
(4, 253),
(42, 53),
(425, 3),
...
(524, 3),
(5, 234),
(52, 34),
(523, 4)]
如果列表是 [4,3,2,5]
因此对将是 (43, 25), (3, 245), (53, 24)
因为它们是列表中每个元素的成对组合
我的问题是,如何获得这些对的一般列表的上述对?
对于原始列表的每个排列,取每个分区。这看起来像:
from itertools import permutations
l = [4,3,2,5]
def combos(l):
for p in permutations(l):
p = "".join(map(str, p))
yield from ((int(p[:n]), int(p[n:])) for n in range(1, len(p)))
list(combos(l))
这导致:
[(4, 325),
(43, 25),
(432, 5),
(4, 352),
(43, 52),
(435, 2),
(4, 235),
(42, 35),
(423, 5),
(4, 253),
(42, 53),
(425, 3),
...
(524, 3),
(5, 234),
(52, 34),
(523, 4)]