Problem splitting a string

Kent Johnson kent37 at tds.net
Sat Oct 15 09:22:21 EDT 2005


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 "<stdin>", line 1, in ?
> NameError: name 'y' is not defined

The order of the 'for' clauses is backwards:
 >>> [x for y in mystr.split(' ') for x in y.split('_')]
['this', 'NP', 'is', 'VL', 'funny', 'JJ']

Kent



More information about the Python-list mailing list