python - Splitting a word into all possible 'subwords' - All possible combinations -


i trying split word subwords - possible permutations.

input: bang output: [['b','ang'], ['ba','ng'], ['ban','g'], ['b','a','ng'], ['b','an','g'], ['ba','n','g'], ['b','a','n','g']] 

i hope covered possbile ways arrange 'bang'. thought long not find way.

list(permutations('bang', 3)) 

permutations method not return entire word. can split 2 words unable split word 3 or more(for bigger word).

splitting 2 words can done using below code suggested 1 of members.

[ [word[:i],word[i:]] in range(1,len(word))] 

here ya go..

def combos(s):   if not s:     return   yield (s,)   in range(1, len(s)):     c in combos(s[i:]):       yield (s[:i],) + c  c in combos('bang'):   print c 

output:

('bang',) ('b', 'ang') ('b', 'a', 'ng') ('b', 'a', 'n', 'g') ('b', 'an', 'g') ('ba', 'ng') ('ba', 'n', 'g') ('ban', 'g') 

Comments