Re: Sequence splitting

2009-07-03 Thread Gabriel Genellina
En Fri, 03 Jul 2009 01:58:22 -0300, //phr...@nospam.invalid escribió: Pablo Torres N. tn.pa...@gmail.com writes: def split(seq, func=bool): t = filter(func, seq) f = filter(lambda x: not func(x), seq) return list(t), list(f) That is icky--you're calling func (which

Re: Sequence splitting

2009-07-03 Thread Brad
On Jul 2, 9:40 pm, Pablo Torres N. tn.pa...@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 =

Re: Sequence splitting

2009-07-03 Thread Brad
On Jul 2, 8:17 pm, Pablo Torres N. tn.pa...@gmail.com wrote: This sounds like it belongs to the python-ideas list.  I suggest posting there for better feedback, since the core developers check that list more often than this one. I tried posting on python-ideas and received a You are not

Re: Sequence splitting

2009-07-03 Thread Rickard Lindberg
I tried posting on python-ideas and received a You are not allowed to post to this mailing list reply. Perhaps because I am posting through Google groups? Or maybe one must be an approved member to post? If you got an awaiting moderator approval message you post might appear on the list soon.

Re: Sequence splitting

2009-07-03 Thread Lie Ryan
Brad wrote: On Jul 2, 9:40 pm, Pablo Torres N. tn.pa...@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):

Re: Sequence splitting

2009-07-03 Thread Lie Ryan
Rickard Lindberg wrote: I tried posting on python-ideas and received a You are not allowed to post to this mailing list reply. Perhaps because I am posting through Google groups? Or maybe one must be an approved member to post? If you got an awaiting moderator approval message you post might

Re: Sequence splitting

2009-07-03 Thread Chris Rebert
On Thu, Jul 2, 2009 at 11:31 PM, Bradschi...@gmail.com wrote: On Jul 2, 9:40 pm, Pablo Torres N. tn.pa...@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).  

Re: Sequence splitting

2009-07-03 Thread Steven D'Aprano
On Fri, 03 Jul 2009 01:02:56 -0700, Paul Rubin wrote: Steven D'Aprano st...@remove-this-cybersource.com.au writes: I've never needed such a split function, and I don't like the name, and the functionality isn't general enough. I'd prefer something which splits the input sequence into as many

Re: Sequence splitting

2009-07-03 Thread Paul Rubin
Steven D'Aprano st...@remove-this-cybersource.com.au writes: groupby() works on lists. a = [1,3,4,6,7] from itertools import groupby b = groupby(a, lambda x: x%2==1) # split into even and odd c = list(b) print len(c) 3 d = list(c[1][1])# should be [4,6] print d # oops. [] The

Re: Sequence splitting

2009-07-03 Thread tsangpo
Just a shorter implementation: from itertools import groupby def split(lst, func): gs = groupby(lst, func) return list(gs[True]), list(gs[False]) Lie Ryan lie.1...@gmail.com дÈëÏûÏ¢ÐÂÎÅ:nfi3m.2341$ze1.1...@news-server.bigpond.net.au... Brad wrote: On Jul 2, 9:40 pm, Pablo Torres N.

Re: Sequence splitting

2009-07-03 Thread Paul Moore
2009/7/3 Brad schi...@gmail.com: Perhaps true, but it would be a nice convenience (for me) as a built- in written in either Python or C. Although the default case of a bool function would surely be faster. The chance of getting this accepted as a builtin is essentially zero. To be a builtin,

Re: Sequence splitting

2009-07-03 Thread Pablo Torres N.
On Fri, Jul 3, 2009 at 06:01, Paul Moorep.f.mo...@gmail.com wrote: 2009/7/3 Brad schi...@gmail.com: Perhaps true, but it would be a nice convenience (for me) as a built- in written in either Python or C. Although the default case of a bool function would surely be faster. The chance of

Re: Sequence splitting

2009-07-03 Thread Lie Ryan
tsangpo wrote: Just a shorter implementation: from itertools import groupby def split(lst, func): gs = groupby(lst, func) return list(gs[True]), list(gs[False]) As you're replying to my post, I assume you meant a shorter implementation my function. But it doesn't do the same

Re: Sequence splitting

2009-07-03 Thread Brad
On Jul 3, 12:57 am, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: I've never needed such a split function, and I don't like the name, and the functionality isn't general enough. I'd prefer something which splits the input sequence into as many sublists as necessary, according to

Re: Sequence splitting

2009-07-03 Thread Paul Rubin
Brad schi...@gmail.com writes: Maybe this would be difficult to get into the core, but how about this idea: Rename the current filter function to something like split or partition (which I agree may be a better name) and modify it to return the desired true and false sequences. Then recreate

Re: Sequence splitting

2009-07-03 Thread Lie Ryan
Brad wrote: On Jul 3, 12:57 am, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: I've never needed such a split function, and I don't like the name, and the functionality isn't general enough. I'd prefer something which splits the input sequence into as many sublists as necessary,

Re: Sequence splitting

2009-07-03 Thread Scott David Daniels
Steven D'Aprano wrote: I've never needed such a split function, and I don't like the name, and the functionality isn't general enough. I'd prefer something which splits the input sequence into as many sublists as necessary, according to the output of the key function. Something like

Re: Sequence splitting

2009-07-03 Thread Mel
Steven D'Aprano wrote: The most important difference between my suggestion and that of the OP is that he limited the key function to something which returns a truth value, while I'm looking for something more general which can split the input into an arbitrary number of collated sublists.

Re: Sequence splitting

2009-07-03 Thread Terry Reedy
Brad wrote: On Jul 2, 8:17 pm, Pablo Torres N. tn.pa...@gmail.com wrote: This sounds like it belongs to the python-ideas list. I suggest posting there for better feedback, since the core developers check that list more often than this one. I tried posting on python-ideas and received a You

Re: Sequence splitting

2009-07-03 Thread Terry Reedy
Paul Rubin wrote: Brad schi...@gmail.com writes: Maybe this would be difficult to get into the core, but how about this idea: Rename the current filter function to something like split or partition (which I agree may be a better name) and modify it to return the desired true and false

Re: Sequence splitting

2009-07-03 Thread Paul Rubin
Terry Reedy tjre...@udel.edu writes: This isn't so attractive, since filter takes a sequence input but returns a list. In modern Python (py3), it returns an iterator. Still not much help in the proposed version that would return two iterators. --

Re: Sequence splitting

2009-07-03 Thread MRAB
Mel wrote: Steven D'Aprano wrote: The most important difference between my suggestion and that of the OP is that he limited the key function to something which returns a truth value, while I'm looking for something more general which can split the input into an arbitrary number of collated

Sequence splitting

2009-07-02 Thread schickb
I have fairly often found the need to split a sequence into two groups based on a function result. Much like the existing filter function, but returning a tuple of true, false sequences. In Python, something like: def split(seq, func=None): if func is None: func = bool t, f = [],

Re: Sequence splitting

2009-07-02 Thread Paul Rubin
schickb schi...@gmail.com writes: def split(seq, func=None): if func is None: func = bool t, f = [], [] for item in seq: if func(item): t.append(item) else: f.append(item) return (t, f) untested: def split(seq,

Re: Sequence splitting

2009-07-02 Thread Pablo Torres N.
On Thu, Jul 2, 2009 at 21:56, schickbschi...@gmail.com wrote: I have fairly often found the need to split a sequence into two groups based on a function result. Much like the existing filter function, but returning a tuple of true, false sequences. In Python, something like: def split(seq,

Re: Sequence splitting

2009-07-02 Thread Pablo Torres N.
On Jul 2, 9:56 pm, schickb schi...@gmail.com wrote: I have fairly often found the need to split a sequence into two groups based on a function result. Much like the existing filter function, but returning a tuple of true, false sequences. In Python, something like: def split(seq, func=None):

Re: Sequence splitting

2009-07-02 Thread Brad
On Jul 2, 8:14 pm, Paul Rubin http://phr...@nospam.invalid wrote: schickb schi...@gmail.com writes: def split(seq, func=None):     if func is None:         func = bool     t, f = [], []     for item in seq:         if func(item):             t.append(item)         else:          

Re: Sequence splitting

2009-07-02 Thread Brad
On Jul 2, 8:17 pm, Pablo Torres N. tn.pa...@gmail.com wrote: On Jul 2, 9:56 pm, schickb schi...@gmail.com wrote: I have fairly often found the need to split a sequence into two groups based on a function result. This sounds like it belongs to the python-ideas list.  I suggest posting

Re: Sequence splitting

2009-07-02 Thread Paul Rubin
Brad schi...@gmail.com writes: On Jul 2, 8:14 pm, Paul Rubin http://phr...@nospam.invalid wrote: schickb schi...@gmail.com writes: def split(seq, func=None):     if func is None:         func = bool     t, f = [], []     for item in seq:         if func(item):            

Re: Sequence splitting

2009-07-02 Thread Brad
On Jul 2, 9:08 pm, Paul Rubin http://phr...@nospam.invalid wrote: Brad schi...@gmail.com writes: On Jul 2, 8:14 pm, Paul Rubin http://phr...@nospam.invalid wrote: schickb schi...@gmail.com writes: def split(seq, func=None):     if func is None:         func = bool     t, f =

Re: Sequence splitting

2009-07-02 Thread Pablo Torres N.
On Thu, Jul 2, 2009 at 23:34, Bradschi...@gmail.com wrote: On Jul 2, 9:08 pm, Paul Rubin http://phr...@nospam.invalid wrote: Brad schi...@gmail.com writes: On Jul 2, 8:14 pm, Paul Rubin http://phr...@nospam.invalid wrote: schickb schi...@gmail.com writes: def split(seq, func=None):  

Re: Sequence splitting

2009-07-02 Thread Paul Rubin
Pablo Torres N. tn.pa...@gmail.com writes: def split(seq, func=bool): t = filter(func, seq) f = filter(lambda x: not func(x), seq) return list(t), list(f) That is icky--you're calling func (which might be slow) twice instead of once on every element of the seq. --