2

I am trying to find a way to create an array where I would have all the variations made out of a set of characters

given A B C, I want to have all possible n-elements combination
for example

A A A B B
A B C A A
A B C A B
A B C B A
etc.

is there any function which would give me something like this? I had a look at the print list(itertools.permutations('ABC',5)) but it seems not to work as the 5 is bigger then the number of given characters.

Chris
  • 759
  • 1
  • 7
  • 20
  • 3
    How about `product('ABC', repeat=5)`? – Hai Vu Nov 01 '16 at 03:36
  • 1
    Possible duplicate of [How to get all possible combinations of a list’s elements?](http://stackoverflow.com/questions/464864/how-to-get-all-possible-combinations-of-a-list-s-elements) – Amadan Nov 01 '16 at 03:37
  • 1
    `list(itertools.product('ABC', repeat=5))` should work for your use case. – Amadan Nov 01 '16 at 03:44
  • 3
    It is not clear what you want. Your description hints at the cartesian power like @HaiVu suggested. However, your example output doesn't make any sense. Why is 'A B C A A' repeated 3 times? – lazy dog Nov 01 '16 at 03:45
  • @ friendly dog, this was a typo, I corrected that. I mean different variations of n elements created out of k characters. Thanks – Chris Nov 01 '16 at 12:51

1 Answers1

4

I believe what you are looking for is product:

list(product('ABC', repeat=5))

Output:

[('A', 'A', 'A', 'A', 'A'),
 ('A', 'A', 'A', 'A', 'B'),
 ('A', 'A', 'A', 'A', 'C'),
 ('A', 'A', 'A', 'B', 'A'),
 ('A', 'A', 'A', 'B', 'B'),
 ('A', 'A', 'A', 'B', 'C'),
 ('A', 'A', 'A', 'C', 'A'),
 ('A', 'A', 'A', 'C', 'B'),
 ...
Hai Vu
  • 34,189
  • 11
  • 57
  • 87