Good question! Here's an efficient method that generates them systematically, without repetition (and without the need to check):
- First, rearrange the first elements
n
; - then permutations , including the
n+1
st-element and the n-1
previous ones, - then those associated with the
n+2
nd element and n-1
previous ones, etc.
, . (, ).
, : 1 base
n-1
.
from itertools import islice, permutations, combinations
def step_permutations(source, n):
"""Return a potentially infinite number of permutations, in forward order"""
isource = iter(source)
base = tuple(islice(isource, n-1))
for x in isource:
for subset in combinations(base, n-1):
for perm in permutations(subset + (x,), n):
yield perm
base += (x,)
:
>>> for p in step_permutations(itertools.count(1), 3):
print(p)
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
(1, 2, 4)
(1, 4, 2)
(2, 1, 4)
(2, 4, 1)
(4, 1, 2)
(4, 2, 1)
(1, 3, 4)
(1, 4, 3)
(3, 1, 4)
(3, 4, 1)
(4, 1, 3)
(4, 3, 1)
(2, 3, 4)
(2, 4, 3)
(3, 2, 4)
...