Sequence splitting

MRAB python at mrabarnett.plus.com
Fri Jul 3 16:07:50 EDT 2009


Mel wrote:
> Steven D'Aprano wrote:
> 
>> The most important difference between my suggestion and that of the OP is
>> that he limited the key function to something which returns a truth
>> value, while I'm looking for something more general which can split the
>> input into an arbitrary number of collated sublists.
> 
> Generally along the lines of
> 
> def scatter (seq, criterion):
>     lists = {}
>     for s in seq:
>         lists.setdefault (criterion (s), []).append (s)
>     return lists
> 
> modulo a defaultdict or some such ?
> 
Somehow this reminds me of the new Counter class, except that it's
making lists instead of counts.



More information about the Python-list mailing list