Reputation: 1218
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))]
where w
is the variable which contains the word. So if w='house'
then this will return [('','house'),('h','ouse')
etc..
This works perfectly for splitting a string into all possible pairs of strings, but now I want to make other splits (e.g. n=3
) of strings such as 'ho','u','se'
which split a single string into all possible ways of n
substrings. How can I do that efficiently?
Upvotes: 4
Views: 65
Reputation: 500635
Here is one way to do this, recursively, using a generator:
def split_str(s, n):
if n == 1:
yield (s,)
else:
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):
Upvotes: 4