Sequence splitting

Chris Rebert clp2 at rebertia.com
Fri Jul 3 04:12:20 EDT 2009


On Thu, Jul 2, 2009 at 11:31 PM, Brad<schickb at gmail.com> wrote:
> On Jul 2, 9:40 pm, "Pablo Torres N." <tn.pa... at gmail.com> wrote:
>>
>> If it is speed that we are after, it's my understanding that map and
>> filter are faster than iterating with the for statement (and also
>> faster than list comprehensions).  So here is a rewrite:
>>
>> def split(seq, func=bool):
>>         t = filter(func, seq)
>>         f = filter(lambda x: not func(x), seq)
>>         return list(t), list(f)
>>
>
> In my simple tests, that takes 1.8x as long as the original solution.
> Better than the itertools solution, when "func" is short and fast. I
> think the solution here would worse if func was more complex.
>
> Either way, what I am still wondering is if people would find a built-
> in implementation useful?

FWIW, Ruby has Enumerable#partition, which does the same thing as
split() and has a better name IMHO.
http://www.ruby-doc.org/core/classes/Enumerable.html#M003130

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list