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 subl

Re: Sequence splitting

2009-07-03 Thread Paul Rubin
Terry Reedy 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. -- http://mail.python.org/mailman/listinfo/python-list

Re: Sequence splitting

2009-07-03 Thread Terry Reedy
Paul Rubin wrote: Brad 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 recreat

Re: Sequence splitting

2009-07-03 Thread Terry Reedy
Brad wrote: On Jul 2, 8:17 pm, "Pablo Torres N." 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 allowed t

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 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 itertools.g

Re: Sequence splitting

2009-07-03 Thread Lie Ryan
Brad wrote: > On Jul 3, 12:57 am, Steven D'Aprano 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 t

Re: Sequence splitting

2009-07-03 Thread Paul Rubin
Brad 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 the > exist

Re: Sequence splitting

2009-07-03 Thread Brad
On Jul 3, 12:57 am, 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.

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 sam

Re: Sequence splitting

2009-07-03 Thread Pablo Torres N.
On Fri, Jul 3, 2009 at 06:01, Paul Moore wrote: > 2009/7/3 Brad : >> 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 builti

Re: Sequence splitting

2009-07-03 Thread Paul Moore
2009/7/3 Brad : > 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, as opposed to be

Re: Sequence splitting

2009-07-03 Thread Steven D'Aprano
On Fri, 03 Jul 2009 01:39:27 -0700, Paul Rubin wrote: > Steven D'Aprano 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])

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" дÈëÏûÏ¢ÐÂÎÅ:nfi3m.2341$ze1.1...@news-server.bigpond.net.au... > Brad wrote: >> On Jul 2, 9:40 pm, "Pablo Torres N." wrote: >>>

Re: Sequence splitting

2009-07-03 Thread Paul Rubin
Steven D'Aprano 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 difference between

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 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 sublists as necessary, accordi

Re: Sequence splitting

2009-07-03 Thread Chris Rebert
On Thu, Jul 2, 2009 at 11:31 PM, Brad wrote: > On Jul 2, 9:40 pm, "Pablo Torres N." 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: >>

Re: Sequence splitting

2009-07-03 Thread Paul Rubin
Steven D'Aprano 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 sublists as necessary, according to the > output of the key function. Something like it

Re: Sequence splitting

2009-07-03 Thread Steven D'Aprano
On Thu, 02 Jul 2009 22:10:14 -0500, Pablo Torres N. 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. If you post to python-ideas, you'll probably be told to gather f

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

Re: Sequence splitting

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

Re: Sequence splitting

2009-07-02 Thread Brad
On Jul 2, 8:17 pm, "Pablo Torres N." 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 allowed to post t

Re: Sequence splitting

2009-07-02 Thread Brad
On Jul 2, 9:40 pm, "Pablo Torres N." 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 = filter(fu

Re: Sequence splitting

2009-07-02 Thread Gabriel Genellina
En Fri, 03 Jul 2009 01:58:22 -0300, > escribió: "Pablo Torres N." 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 e

Re: Sequence splitting

2009-07-02 Thread Paul Rubin
"Pablo Torres N." 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. -- http://mail.python.org

Re: Sequence splitting

2009-07-02 Thread Pablo Torres N.
On Thu, Jul 2, 2009 at 23:34, Brad wrote: > On Jul 2, 9:08 pm, Paul Rubin wrote: >> Brad writes: >> > On Jul 2, 8:14 pm, Paul Rubin wrote: >> > > schickb writes: >> > > > def split(seq, func=None): >> > > >     if func is None: >> > >

Re: Sequence splitting

2009-07-02 Thread Brad
On Jul 2, 9:08 pm, Paul Rubin wrote: > Brad writes: > > On Jul 2, 8:14 pm, Paul Rubin wrote: > > > schickb writes: > > > > def split(seq, func=None): > > > >     if func is None: > > > >         func = bool > > > >     t, f = [], [] >

Re: Sequence splitting

2009-07-02 Thread Paul Rubin
Brad writes: > On Jul 2, 8:14 pm, Paul Rubin wrote: > > schickb writes: > > > def split(seq, func=None): > > >     if func is None: > > >         func = bool > > >     t, f = [], [] > > >     for item in seq: > > >         if func(item): > > >             t.append(

Re: Sequence splitting

2009-07-02 Thread Brad
On Jul 2, 8:17 pm, "Pablo Torres N." wrote: > On Jul 2, 9:56 pm, schickb 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 there for better feedback, si

Re: Sequence splitting

2009-07-02 Thread Brad
On Jul 2, 8:14 pm, Paul Rubin wrote: > schickb 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 Pablo Torres N.
On Jul 2, 9:56 pm, schickb 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): >     if fu

Re: Sequence splitting

2009-07-02 Thread Pablo Torres N.
On Thu, Jul 2, 2009 at 21:56, schickb 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 Paul Rubin
schickb 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, func=bool):

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 = [], [