mwh at python.net
Thu Jun 13 17:00:28 CEST 2002
Oleg Broytmann <phd at phd.pp.ru> writes:
> I want to have new python builtin - filter2. It is exactly like filter,
> but it returns also a list of items that didn't pass the test.
> Of course I can do it with just "for" loop:
> l1 = 
> l2 = 
> for item in alist:
> if test(item):
> but this way I am missing filter's loop written in C.
Not much, I suspect.
> Running filter two times (with test and inverted test) is not an option,
> as test could be a costly function.
Write it in Python. Particularly if test is expensive the loop
overhead will disappear into the noise.
If you insist on C loops, there's always:
lt = map(lambda x:(test(x), x), alist)
l1 = filter(lambda (x,y):x, lt)
l2 = filter(lambda (x,y):not x, lt)
But I doubt you need me to tell you why this solution sucks.)
I'm not sure that the ability to create routing diagrams
similar to pretzels with mad cow disease is actually a
marketable skill. -- Steve Levin
More information about the Python-list