Héctor van den Boorn Héctor van den Boorn - 1 year ago 65
Python Question

Creating an exhaustive list of `n` substrings

I am training to split a string into n substrings and return a list of tuples of them.

I now use the code

for (w1,w2) in [(w[:i],w[i:]) for i in range(len(w))]
is the variable which contains the word. So if
then this will return

This works perfectly for splitting a string into all possible pairs of strings, but now I want to make other splits (e.g.
) of strings such as
which split a single string into all possible ways of
substrings. How can I do that efficiently?

Answer Source

Here is one way to do this, recursively, using a generator:

def split_str(s, n):
  if n == 1:
     yield (s,)
    for i in range(len(s)):
      left, right = s[:i], s[i:]
      for substrings in split_str(right, n - 1):
        yield (left,) + substrings

for substrings in split_str('house', 3):
  print substrings

This prints out:

('', '', 'house')
('', 'h', 'ouse')
('', 'ho', 'use')
('', 'hou', 'se')
('', 'hous', 'e')
('h', '', 'ouse')
('h', 'o', 'use')
('h', 'ou', 'se')
('h', 'ous', 'e')
('ho', '', 'use')
('ho', 'u', 'se')
('ho', 'us', 'e')
('hou', '', 'se')
('hou', 's', 'e')
('hous', '', 'e')

If you don't want the empty strings, change the loop bounds to

    for i in range(1, len(s) - n + 2):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download