Possible Duplicate:
is there any best way to generate all possible three letters keywords
how can I enumerate all strings of length K from an alphabet L, where L is simply a list of characters? E.g. if L = ['A', 'B', 'C']
and K = 2
, I'd like to enumerate all possible strings of length 2 that can be made up with the letters 'A'
, 'B'
, 'C'
. They can be reused, so 'AA'
is valid.
This is essentially permutations with replacement, as far as I understand. if theres a more correct technical term for this, please let me know.... its essentially all strings of length K that you can make by choosing ANY letter from the alphabet L, and possibly reusing letters, in a way that is sensitive to order (so AB
is NOT identical to BA
according to this.) is there a clearer way to state this?
in any case i believe the solution is:
[ ''.join(x) for x in product(L, repeat=K) ]
but i am interested in other answers to this, esp. naive approaches versus fast Pythonic ones, and discussions of speed considerations.