Problem splitting a string

Alex Martelli aleax at mail.comcast.net
Sat Oct 15 04:51:41 EDT 2005


Steven D'Aprano <steve at REMOVETHIScyber.com.au> 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. 

....because the flattening is easy:

[ x for x in y.split('_') for y in z.split(' ') ]


Alex



More information about the Python-list mailing list