list partition

Hameed Khan hameedkhaan at yahoo.com
Sun Feb 22 01:45:51 EST 2004


hi Moosebump,
       the following is a function i have return for
this purpose. hope this will help you.


>>> def newfilter(func, list):
...     flist = filter(func, list)
...     slist = filter(lambda x: not x in flist, list)
...     return flist, slist
...
>>> newfilter(lambda x: x>0, a)
([1, 2, 3], [0, 0])

Thanks,
Hameed.

----------Original Message--------------

Message: 4
Date: Sat, 21 Feb 2004 22:37:44 GMT
From: "Moosebumps" <Moosebumps at Moosebumps.Moosebumps>
Subject: list partition
To: python-list at python.org
Message-ID:
<Y4RZb.28632$x57.632 at newssvr25.news.prodigy.com>

Is there a function that takes a list and predicate,
and returns two
lists -- one for which the predicate is true and one
for which it is 
false?

I know I can call filter(p, list) and filter( not p,
list) or something 
like
that, but I assume it would be more efficient to do it
in one pass.

Actually I probably can't call "not p", I would have
to wrap p in a 
function
notP or something.  Or is there a shortcut for
lambda(x) : not p(x)?  I
guess it isn't that long : )

MB



=====
_____________________
 Hameed Ullah Khan

*Try not to become a man of success but rather to become a man of value.*
Albert Einstein

__________________________________
Do you Yahoo!?
Yahoo! Mail SpamGuard - Read only the mail you want.
http://antispam.yahoo.com/tools




More information about the Python-list mailing list