Re: Partition list with predicate

2008-04-23 Thread Steve Holden
Jared Grubb wrote: 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 ob

Re: Partition list with predicate

2008-04-23 Thread Jared Grubb
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", wh

Re: Partition list with predicate

2008-04-23 Thread Brian
On Wed, Apr 23, 2008 at 9:08 PM, Jared Grubb <[EMAIL PROTECTED]> 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

Re: Partition list with predicate

2008-04-23 Thread Jared Grubb
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 cl

Re: Partition list with predicate

2008-04-23 Thread castironpi
On Apr 23, 2:26 pm, Steve Holden <[EMAIL PROTECTED]> wrote: > Terry Reedy wrote: > > "Jared Grubb" <[EMAIL PROTECTED]> wrote in message > >news:[EMAIL PROTECTED] > > | I want a function that removes values from a list if a predicate > > evaluates > > | to True. > > > Forget the rigamarole you poste

Re: Partition list with predicate

2008-04-23 Thread Steve Holden
Terry Reedy wrote: "Jared Grubb" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | I want a function that removes values from a list if a predicate evaluates | to True. Forget the rigamarole you posted, which has several defects. If you must modify the list in place, because you ha

Re: Partition list with predicate

2008-04-23 Thread Terry Reedy
"Jared Grubb" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | I want a function that removes values from a list if a predicate evaluates | to True. Forget the rigamarole you posted, which has several defects. If you must modify the list in place, because you have multiple referenc

Re: Partition list with predicate

2008-04-23 Thread Tim Golden
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

Partition list with predicate

2008-04-23 Thread Jared Grubb
I want a function that removes values from a list if a predicate evaluates to True. The best I could come up with is: def extract(lst, pred): idx = 0 ret = [] for obj in lst[:]: if pred(obj): ret.append(obj) lst.pop(idx) else: idx +=