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 ?
>
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
"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.
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
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',
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
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.
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
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
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
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
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.
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(
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:
14 matches
Mail list logo