smfiles writes:
> I think it's necessary to add a segment() method to str type or
> string module. This method is used to split a string into m parts
> and return all cases. With segment(), you can avoid tedious
> calculation and indexing if you want to segment a string.
In addition to David's pointers to code using combinations, here's a
one-liner based on more_itertools. Given combinatoric explosion,
it might be better to use a genexp instead of a list as return value,
and an "efficient but convoluted" algorithm might be justified if
len(s) is at all large.
# more_itertools is not in stdlib, but can be pip'ed
from more_itertools import partitions as p
def segment(s, m):
return [tuple("".join(y) for y in x) for x in p(s) if len(x) == m]
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/GJ6FICIID7MEX4O2JRVOLJFOT5PSO46J/
Code of Conduct: http://python.org/psf/codeofconduct/