I have a number of letters say three a's and two b's and I want to find all possible words with them. I have tried itertools.permutations
and itertools.product
but they didn't help because:
1) The results permutations
contains repetitions (i.e. the same word appears multiple times). For example:
> print [''.join(i) for i in itertools.permutations('aab', 3)]
['aab', 'aba', 'aab', 'aba', 'baa', 'baa']
2) The results of product
can contain words with only one of the letters:
> print [''.join(i) for i in itertools.product('ab', repeat=3)]
['aaa', 'aab', 'aba', 'abb', 'baa', 'bab', 'bba', 'bbb']
With two a's and one b I want to get `['aab', 'aba', 'baa']. Also I need the approach to use iterators and not lists (or any other way the stores everything in memory) because the results can be very large.