tsangpo wrote:
> Just a shorter implementation:
> 
> from itertools import groupby
> def split(lst, func):
>     gs = groupby(lst, func)
>     return list(gs[True]), list(gs[False])
> 

As you're replying to my post, I assume you meant a shorter
implementation my function. But it doesn't do the same thing. The idea
with my group() is similar to what Steven D'Aprano is describing in
another branch of this thread (i.e. splitting not only based on True and
False, but arbitrary groupings, e.g. 'tru', 'flash' or perhaps -1, 0, 1).

For example:
>>> def group(seq, func=bool):
...     ret = {}
...     for item in seq:
...         fitem = func(item)
...         try:
...             ret[fitem].append(item)
...         except KeyError:
...             ret[fitem] = [item]
...     return ret
...
>>> group(range(10), lambda x: x%3)
{0: [0, 3, 6, 9], 1: [1, 4, 7], 2: [2, 5, 8]}
>>> # the next one is the OP's split
>>> group(['true', '', [], [''], 'false'], bool)
{False: ['', []], True: ['true', [''], 'false']}
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to