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
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 =
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
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.
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):
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
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).
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
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
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.
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,
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
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
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
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
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,
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
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.
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
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
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.
--
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
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 = [],
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,
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,
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):
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:
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
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):
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 =
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):
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.
--
32 matches
Mail list logo