I made a search for stackoverflow about this but couldn't find a way to do it. It probably involves itertools.
I want to find all the possible results of splitting a string, say the string thisisateststring
into n
(equal or unequal length, doesn't matter, both should be included) strings.
For example let n
be 3
:
[["thisisat", "eststrin", "g"], ["th", "isisates", "tstring"], ............]
You can use itertools.combinations
here. You simply need to pick two splitting points to generate each resulting string:
from itertools import combinations
s = "thisisateststring"
pools = range(1, len(s))
res = [[s[:p], s[p:q], s[q:]] for p, q in combinations(pools, 2)]
print res[0]
print res[-1]
Output:
['t', 'h', 'isisateststring']
['thisisateststri', 'n', 'g']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With