This will give a result that is more than a permutation, but all possible combinations. An example is when input is 'abc', the output would be: a,ab,abc,ac,acb,b,ba,bac,bc,bca,c,ca,cab,cb,cba
1 2 3 4 5 | def allperm(inputstr):
for i in range(len(inputstr)):
yield(inputstr[i])
for s in allperm(inputstr[:i] + inputstr[i+1:]):
yield(inputstr[i] + s)
|
from itertools import permutations [perm for length in range(1, len(inputstr) + 1) for perm in permutations(inputstr, length)]
oh, seems like i just re-invented the wheel:(
Thanks Ori for the hint,and good job rhettinger ;)
The author's recipe is easier to understand than the 'from itertools import permutations' way.
Nice recipe. This site helped explain it in detail:
Find all permutations of a string