Sequence splitting

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jul 3 03:57:02 EDT 2009


On Thu, 02 Jul 2009 22:10:14 -0500, Pablo Torres N. wrote:

> This sounds like it belongs to the python-ideas list.  I suggest posting
> there for better feedback, since the core developers check that list
> more often than this one.

If you post to python-ideas, you'll probably be told to gather feedback 
here first. The core-developers aren't hugely interested in arbitrary new 
features unless they have significant community support.

I've never needed such a split function, and I don't like the name, and 
the functionality isn't general enough. I'd prefer something which splits 
the input sequence into as many sublists as necessary, according to the 
output of the key function. Something like itertools.groupby(), except it 
runs through the entire sequence and collates all the elements with 
identical keys.

E.g.:

splitby(range(10), lambda n: n%3)
=> [ (0, [0, 3, 6, 9]),
     (1, [1, 4, 7]), 
     (2, [2, 5, 8]) ]

Your split() would be nearly equivalent to this with a key function that 
returns a Boolean.



-- 
Steven





More information about the Python-list mailing list