Problem splitting a string

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Oct 15 03:47:24 EDT 2005


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 string representing all
> delimiters we want to use.

No, the delimiter is the delimiter, not a list of delimiters.

The only exception is delimiter=None, which splits on any whitespace.

[Aside: I think a split-on-any-delimiter function would be useful.]

> I do I split the string by using both ' ' and '_' as
> the delimiters at once?

Something like this:

mystr = 'this_NP is_VL funny_JJ'
L1 = mystr.split()  # splits on whitespace
L2 = []
for item in L1:
    L2.extend(item.split('_')

You can *almost* do that as a one-liner:

L2 = [item.split('_') for item in mystr.split()]

except that gives a list like this:

[['this', 'NP'], ['is', 'VL'], ['funny', 'JJ']]

which needs flattening. 

-- 
Steven.




More information about the Python-list mailing list