Re: Problem splitting a string

2005-10-15 Thread Kent Johnson
Steven D'Aprano wrote: > On Sat, 15 Oct 2005 10:51:41 +0200, Alex Martelli wrote: >>[ x for x in y.split('_') for y in z.split(' ') ] > > py> mystr = 'this_NP is_VL funny_JJ' > py> [x for x in y.split('_') for y in mystr.split(' ')] > Traceback (most recent call last): > File "", line 1, in ? >

Re: Problem splitting a string

2005-10-15 Thread Kent Johnson
Alex Martelli wrote: > Using sum on lists is DEFINITELY slow -- avoid it like the plague. > > If you have a list of lists LOL, DON'T use sum(LOL, []), but rather > > [x for x in y for y in LOL] Should be >>> lol = [[1,2],[3,4]] >>> [x for y in lol for x in y] [1, 2, 3, 4] The outer loop comes

Re: Problem splitting a string

2005-10-15 Thread Fredrik Lundh
"SPE - Stani's Python Editor" wrote: > Use re.split, as this is the fastest and cleanest way. > However, iff you have to split a lot of strings, the best is: > > import re > delimiters = re.compile('_| ') > > def split(x): > return delimiters.split(x) or, shorter: import re split = re.

Re: Problem splitting a string

2005-10-15 Thread SPE - Stani's Python Editor
Use re.split, as this is the fastest and cleanest way. However, iff you have to split a lot of strings, the best is: import re delimiters = re.compile('_| ') def split(x): return delimiters.split(x) >>> split('this_NP is_VL funny_JJ') ['this', 'NP', 'is', 'VL', 'funny', 'JJ'] Stani -- SPE - S

Re: Problem splitting a string

2005-10-15 Thread Steven D'Aprano
On Sat, 15 Oct 2005 10:51:41 +0200, Alex Martelli wrote: > Steven D'Aprano <[EMAIL PROTECTED]> wrote: >... >> You can *almost* do that as a one-liner: > > No 'almost' about it... > >> L2 = [item.split('_') for item in mystr.split()] >> >> except that gives a list like this: >> >> [['this',

Re: Problem splitting a string

2005-10-15 Thread Alex Martelli
Mike Meyer <[EMAIL PROTECTED]> wrote: ... > A third alternative is to split once, then split the substrings a > second time and stitch the results back together: > > >>> sum([x.split('_') for x in mystr.split()], []) > ['this', 'NP', 'is', 'VL', 'funny', 'JJ'] > > Which is probably slow. To ba

Re: Problem splitting a string

2005-10-15 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote: ... > You can *almost* do that as a one-liner: No 'almost' about it... > L2 = [item.split('_') for item in mystr.split()] > > except that gives a list like this: > > [['this', 'NP'], ['is', 'VL'], ['funny', 'JJ']] > > which needs flattening.

Re: Problem splitting a string

2005-10-15 Thread Paul Rubin
Anthony Liu <[EMAIL PROTECTED]> writes: > I do I split the string by using both ' ' and '_' as > the delimiters at once? Use re.split. -- http://mail.python.org/mailman/listinfo/python-list

Re: Problem splitting a string

2005-10-15 Thread Steven D'Aprano
On Fri, 14 Oct 2005 21:52:07 -0700, Anthony Liu wrote: > I have this simple string: > > mystr = 'this_NP is_VL funny_JJ' > > I want to split it and give me a list as > > ['this', 'NP', 'is', 'VL', 'funny', 'JJ'] > I think the documentation does say that the > separator/delimiter can be a strin

Re: Problem splitting a string

2005-10-14 Thread Mike Meyer
Robert Kern <[EMAIL PROTECTED]> writes: > Anthony Liu wrote: >> I have this simple string: >> >> mystr = 'this_NP is_VL funny_JJ' >> >> I want to split it and give me a list as >> >> ['this', 'NP', 'is', 'VL', 'funny', 'JJ'] > You could use regular expressions as Jason Stitt mentions, or you cou

Re: Problem splitting a string

2005-10-14 Thread Robert Kern
Anthony Liu wrote: > I have this simple string: > > mystr = 'this_NP is_VL funny_JJ' > > I want to split it and give me a list as > > ['this', 'NP', 'is', 'VL', 'funny', 'JJ'] > I think the documentation does say that the > separator/delimiter can be a string representing all > delimiters we wa

Re: Problem splitting a string

2005-10-14 Thread Erik Max Francis
Anthony Liu wrote: > I have this simple string: > > mystr = 'this_NP is_VL funny_JJ' > > I want to split it and give me a list as > > ['this', 'NP', 'is', 'VL', 'funny', 'JJ'] > > 1. I tried mystr.split('_| '), but this gave me: > > ['this_NP is_VL funny_JJ'] > > It is not splitted at all.

Re: Problem splitting a string

2005-10-14 Thread Jason Stitt
On Oct 14, 2005, at 11:52 PM, Anthony Liu wrote:I have this simple string:mystr = 'this_NP is_VL funny_JJ'I want to split it and give me a list as['this', 'NP', 'is', 'VL', 'funny', 'JJ']1. I tried mystr.split('_| '), but this gave me:['this_NP is_VL funny_JJ']Try re.split, as in:import rere.split(

Problem splitting a string

2005-10-14 Thread Anthony Liu
I have this simple string: mystr = 'this_NP is_VL funny_JJ' I want to split it and give me a list as ['this', 'NP', 'is', 'VL', 'funny', 'JJ'] 1. I tried mystr.split('_| '), but this gave me: ['this_NP is_VL funny_JJ'] It is not splitted at all. 2. I tried mystr.split('_'), and this gave me: