classification
Title: Segmentation of string
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: SilentGhost, lovi, mark.dickinson
Priority: normal Keywords:

Created on 2019-12-14 07:54 by lovi, last changed 2019-12-14 18:22 by mark.dickinson. This issue is now closed.

Messages (3)
msg358383 - (view) Author: Lovi (lovi) Date: 2019-12-14 07:54
I thought for a long time. 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.

For example:

segment('1234', m=3) -> [('1', '2', '34'), ('1', '23', '4'), ('12', '3', '4')]
segment('12345', m=3) -> [('1', '2', '345'), ('1', '23', '45'), ('1', '234', '5'), ('12', '3', '45'), ('12', '34', '5'), ('123', '4', '5')]


I hope this proposal can be adopted.
msg358385 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-12-14 08:26
It is generally suggested to offer this sort of proposals for discussion on python-ideas mailing list [0] first. There, you can elaborate on why you think this is necessary, what sort of use cases this new method could have, etc. Once there is a broad agreement on behaviour and details of this new feature, if it's to be adopted at all, you can open an issue to track the implementation work.

Good luck. I'm closing the issue, as the proposal is not yet well formed, but you're welcome to re-open this bug after discussion on the mailing list.

[0] https://mail.python.org/mailman3/lists/python-ideas.python.org/
msg358400 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2019-12-14 18:22
For the record, this is an easy application of itertools.combinations:

>>> def segment(s, m):
...     for c in itertools.combinations(range(1, len(s)), m-1):
...         yield tuple(s[i:j] for i, j in zip((0,)+c, c+(len(s),)))
... 
>>> list(segment("12345", m=3))
[('1', '2', '345'), ('1', '23', '45'), ('1', '234', '5'), ('12', '3', '45'), ('12', '34', '5'), ('123', '4', '5')]
History
Date User Action Args
2019-12-14 18:22:37mark.dickinsonsetnosy: + mark.dickinson
messages: + msg358400
2019-12-14 08:26:01SilentGhostsetstatus: open -> closed

nosy: + SilentGhost
messages: + msg358385

resolution: not a bug
stage: resolved
2019-12-14 07:54:44lovicreate