Below is a "standard recursive answer", similar to the other similar answer http://stackoverflow.com/a/23743696/711085 . We(We don't realistically have to worry about running out of stack space since there's no way we could process all N! permutations, though if you were making the "get k-rank permutation / successor permutation" function, it would avoid this problem.)
def permscombs(xs, i=0):
if xs==[]i==len(xs):
yield []()
return
for i,xc in enumeratecombs(xs,i+1):
subset =yield list(xs)c
subset.popyield c+(ixs[i],)
Demo:
>>> list( combs(range(5)) )
[(), (0,), (1,), (1, 0), for(2,), p(2, in0), perms(subset2, 1):
, (2, 1, 0), (3,), (3, 0), (3, 1), (3, 1, 0), yield(3, p+[x]2), (3, #2, could0), reverse(3, order2, may1), be(3, slightly2, less1, efficient
Demo:
>>>0), list(perms4,), ([14,2 0),3 (4,4] 1), (4, 1, 0)
[[4, 3(4, 2), 1](4, [32, 0), (4, 2, 1]1), [4(4, 2, 31, 1]0), [2(4, 3), (4, 3, 1]0),
(4, [33, 21), (4, 1]3, [21, 0), (4, 3, 2), (4, 1]3, [42, 0), (4, 3, 2, 1), 2](4, [33, 42, 1, 2]0)]
>>> list(sorted( combs(range(5)), key=len))
[(), [4
(0,), (1,), (2,), (3,), 2](4,), [1
(1, 40), (2, 0), (2, 1), (3, 2]0), [3(3, 1), (3, 2), (4, 2]0), [1(4, 31), (4, 2]2),
(4, [43),
(2, 1, 3]0), [2(3, 41, 10), 3](3, [42, 10), (3, 2, 3]1), [1(4, 1, 0), (4, 2, 3]0),
(4, [22, 1), (4, 3]3, [10), 2(4, 3, 1), (4, 3]3, [32),
(3, 2, 1, 4]0), [2(4, 32, 1, 4]0),
[3(4, 13, 21, 4]0), [1(4, 3, 2, 4]0), [2(4, 13, 32, 4]1), [1
(4, 3, 2, 31, 4]]0)]
>>> len(set(tuplecombs(x) for x in permsrange([1,2,3,4]5))))
2432