fork download
  1. from itertools import combinations, chain
  2.  
  3. def split_combinations(L):
  4. for split in range(1, len(L)):
  5. for combination in split_combinations(L[split:]):
  6. yield [L[:split]] + combination
  7. yield [L]
  8.  
  9. def split_combinations2(s):
  10. for i in range(len(s)):
  11. for split_points in combinations(range(1, len(s)), i):
  12. yield split_string(s, split_points)
  13.  
  14. def split_string(s, t):
  15. return [s[start:finish] for start, finish in zip((None, ) + t, t + (None, ))]
  16.  
  17.  
  18. for test in 'abcd', 'abcde':
  19. print (list(split_combinations(test)))
  20. print (list(split_combinations2(test)))
Success #stdin #stdout 0.1s 10088KB
stdin
Standard input is empty
stdout
[['a', 'b', 'c', 'd'], ['a', 'b', 'cd'], ['a', 'bc', 'd'], ['a', 'bcd'], ['ab', 'c', 'd'], ['ab', 'cd'], ['abc', 'd'], ['abcd']]
[['abcd'], ['a', 'bcd'], ['ab', 'cd'], ['abc', 'd'], ['a', 'b', 'cd'], ['a', 'bc', 'd'], ['ab', 'c', 'd'], ['a', 'b', 'c', 'd']]
[['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'de'], ['a', 'b', 'cd', 'e'], ['a', 'b', 'cde'], ['a', 'bc', 'd', 'e'], ['a', 'bc', 'de'], ['a', 'bcd', 'e'], ['a', 'bcde'], ['ab', 'c', 'd', 'e'], ['ab', 'c', 'de'], ['ab', 'cd', 'e'], ['ab', 'cde'], ['abc', 'd', 'e'], ['abc', 'de'], ['abcd', 'e'], ['abcde']]
[['abcde'], ['a', 'bcde'], ['ab', 'cde'], ['abc', 'de'], ['abcd', 'e'], ['a', 'b', 'cde'], ['a', 'bc', 'de'], ['a', 'bcd', 'e'], ['ab', 'c', 'de'], ['ab', 'cd', 'e'], ['abc', 'd', 'e'], ['a', 'b', 'c', 'de'], ['a', 'b', 'cd', 'e'], ['a', 'bc', 'd', 'e'], ['ab', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']]