3

It's easy in python to calculate simple permutations using itertools.permutations().

You can even find some possible permutations of multiple lists.

import itertools
s=[ [ 'a', 'b', 'c'], ['d'], ['e', 'f'] ]
for l in list(itertools.product(*s)):
    print(l)


('a', 'd', 'e')
('a', 'd', 'f')
('b', 'd', 'e')
('b', 'd', 'f')
('c', 'd', 'e')
('c', 'd', 'f')

It's also possible to find permutations of different lengths.

import itertools
s = [1, 2, 3]
for L in range(0, len(s)+1):
    for subset in itertools.combinations(s, L):
        print(subset)

()
(1,)
(2,)
(3,)
(1, 2)
(1, 3)
(2, 3)
(1, 2, 3)

How would you find permutations of all possible 1) lengths, 2) orders, and 3) from multiple lists?

I would assume the first step would be to combine the lists into one. A list will not de-dup items like a set would.

s=[ [ 'a', 'b', 'c'], ['d'], ['e', 'f'] ]

('a', 'b')
('a', 'c')
('a', 'd')
('a', 'e')
('a', 'f')
...
('b', 'a')
('c', 'a')
...
('a', 'b', 'c', 'd', 'e')
...
('a', 'b', 'c', 'd', 'e', 'f')
...
('f', 'a', 'b', 'c', 'd', 'e')
Xeoncross
  • 55,620
  • 80
  • 262
  • 364
  • if you flatten the list, doesn't it solve it? – Jean-François Fabre May 08 '18 at 21:02
  • @BcK this most certainly is not a duplicate of questions about flattening lists. If anything, this would be a duplicate of the two questions I already linked to about finding permutations. However, I already explained the differences in the question. – Xeoncross May 09 '18 at 01:40

2 Answers2

3

Like you suggested, do:

s = [x for y in s for x in y]

and then use your solution for finding permutations of different lengths:

for L in range(0, len(s)+1):
    for subset in itertools.combinations(s, L):
        print(subset)

would find:

()
('a',)
('b',)
('c',)
('d',)
('e',)
('f',)
('a', 'b')
('a', 'c')
('a', 'd')
('a', 'e')
('a', 'f')
('b', 'c')
('b', 'd')
('b', 'e')
('b', 'f')
('c', 'd')
('c', 'e')
('c', 'f')
('d', 'e')
('d', 'f')
('e', 'f')
('a', 'b', 'c')
('a', 'b', 'd')
('a', 'b', 'e')
('a', 'b', 'f')
('a', 'c', 'd')
('a', 'c', 'e')
('a', 'c', 'f')
('a', 'd', 'e')
('a', 'd', 'f')
('a', 'e', 'f')
('b', 'c', 'd')
('b', 'c', 'e')
('b', 'c', 'f')
('b', 'd', 'e')
('b', 'd', 'f')
('b', 'e', 'f')
('c', 'd', 'e')
('c', 'd', 'f')
('c', 'e', 'f')
('d', 'e', 'f')
('a', 'b', 'c', 'd')
('a', 'b', 'c', 'e')
('a', 'b', 'c', 'f')
('a', 'b', 'd', 'e')
('a', 'b', 'd', 'f')
('a', 'b', 'e', 'f')
('a', 'c', 'd', 'e')
('a', 'c', 'd', 'f')
('a', 'c', 'e', 'f')
('a', 'd', 'e', 'f')
('b', 'c', 'd', 'e')
('b', 'c', 'd', 'f')
('b', 'c', 'e', 'f')
('b', 'd', 'e', 'f')
('c', 'd', 'e', 'f')
('a', 'b', 'c', 'd', 'e')
('a', 'b', 'c', 'd', 'f')
('a', 'b', 'c', 'e', 'f')
('a', 'b', 'd', 'e', 'f')
('a', 'c', 'd', 'e', 'f')
('b', 'c', 'd', 'e', 'f')
('a', 'b', 'c', 'd', 'e', 'f')

If you want to distinguish e.g. ('d', 'e', 'f') from ('f', 'e', 'd') (thanks @Kefeng91 for pointing this out) and others, replace itertools.combinations with itertools.permutations, like @YakymPirozhenko suggests.

fferri
  • 18,285
  • 5
  • 46
  • 95
2

Here's a simple one liner (You can replace feature_cols instead of s)

Combinations:

[combo for i in range(1, len(feature_cols) + 1) for combo in itertools.combinations(feature_cols, i) ]

Permutations:

[combo for i in range(1, len(feature_cols) + 1) for combo in itertools.permutations(feature_cols, i) ]

See my answer here for more details

A H
  • 2,164
  • 1
  • 21
  • 36