Here's a discussion of both a conceptually simple and a convoluted but fast version (the latter, naturally, by Tim Peters). This is for integer partitions, but compositions are simply the permutations on the full length of the list of each partition.
http://code.activestate.com/recipes/218332-generator-for-integer-partitions/ Maybe Timmy will provide something more efficient than his partition code combined with itertools.permutation. I'm not sure if such shortcuts exist myself. On Sat, Dec 14, 2019, 10:41 AM David Mertz <me...@gnosis.cx> wrote: > This feels much too special purpose for a string method, and probably for > anything in the standard library. I'm not sure when someone would want this. > > But it's an only very sightly special case of integer composition ( > https://en.wikipedia.org/wiki/Composition_(combinatorics)). And related > to that, of course is integer partition. > > If we had a composition() function (maybe in itertools... but more likely > in more-itertools given the minimalist philosophy of itertools itself), > this problem is simply taking slices whose lengths are drawn from the > composition. > > On Sat, Dec 14, 2019, 9:44 AM smfiles <1668151...@qq.com> wrote: > >> 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. >> >> 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. >> _______________________________________________ >> Python-ideas mailing list -- python-ideas@python.org >> To unsubscribe send an email to python-ideas-le...@python.org >> https://mail.python.org/mailman3/lists/python-ideas.python.org/ >> Message archived at >> https://mail.python.org/archives/list/python-ideas@python.org/message/VYDU3JF2MFFUUHU3XUCIAW3UXJGXZCKD/ >> Code of Conduct: http://python.org/psf/codeofconduct/ >> >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/OYDTLMKRTFNC435F7TY5ZAN3D7YONXWQ/ Code of Conduct: http://python.org/psf/codeofconduct/