Problem splitting a string
mwm at mired.org
Sat Oct 15 08:07:51 CEST 2005
Robert Kern <robert.kern at gmail.com> 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 could
> replace '_' with ' ' and then split.
> In : mystr = 'this_NP is_VL funny_JJ'
> In : mystr.replace('_', ' ').split()
> Out: ['this', 'NP', 'is', 'VL', 'funny', 'JJ']
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 bad extend doesn't take multiple arguments.
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list