Partition list with predicate

Jared Grubb jared.grubb at gmail.com
Wed Apr 23 21:39:46 EDT 2008


That almost works, except that a predicate can have "memory". For example,
the predicate could be "extract every other object", which doesn't care
about the value of the object (so you cant remove them later based on
value!). Or your predicate might be "remove the first 5 objects that are
even", which means that the predicate must be run against the list in
forward order with only one test per element (like an input/output iterator
in C++; running the predicate changes the predicate itself, so you cant run
one pass with "pred" and then another pass with "not pred" to get the rest).
Example of a case where your proposed solution wouldn't quite work:
class EveryOtherOne:
   def __init__(self):
      self.parity = False
   def __call__(self, obj):
      ret, self.parity = self.parity, not self.parity
      return ret

>>> pred = EveryOtherOne()
>>> lst = [1,2,2,1]
>>> extracted = [ obj for obj in lst if pred(obj) ]
>>> extracted
[2, 1]
>>> lst = [ obj for obj in lst if obj not in extracted ]
>>> lst
[]

On Wed, Apr 23, 2008 at 6:14 PM, Brian <breily at gmail.com> wrote:

> On Wed, Apr 23, 2008 at 9:08 PM, Jared Grubb <jared.grubb at gmail.com>
> wrote:
>
> >  I guess I forgot one requirement: the removed elements need to be
> > remembered.
> >
> > Basically, I have a list of objects in a buffer, one class operates on
> > some of the objects, but other classes use others. So, a class must extract
> > the ones it can handle, and leave the rest in the buffer for the other
> > classes to handle.
> >
> > I haven't found a function that will both remove objects from a list,
> > but save the ones that do get removed.
> >
> > Jared
> >
> > On 23 Apr 2008, at 10:15, Tim Golden wrote:
> >
> > Jared Grubb wrote:
> >
> > I want a function that removes values from a list if a predicate
> > evaluates to True. The best I could come up with is:
> >
> >
> > Have a look at the itertools module, and the ifilter function
> > in particular.
> >
> > TJG
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> >
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
> I would do it like this:
>
> # This takes out the values
> extracted = [ obj for obj in lst if pred(obj) ]
> # This filters out any item that was extracted
> lst = [ obj for obj in list if obj not in extracted ]
>
> Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080423/51d4bdb5/attachment.html>


More information about the Python-list mailing list