newby question: Splitting a string - separator

James Stroud jstroud at mbi.ucla.edu
Fri Dec 9 23:24:55 EST 2005


Kent Johnson wrote:
> James Stroud wrote:
> 
>> The one I like best goes like this:
>>
>> py> data = "Guido van Rossum  Tim Peters     Thomas Liesner"
>> py> names = [n for n in data.split() if n]
>> py> names
>> ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']
>>
>> I think it is theoretically faster (and more pythonic) than using 
>> regexes.
> 
> 
> Unfortunately it gives the wrong result.
> 
> Kent

Just an example. Here is the "correct version":


names = [n for n in data.split("  ") if n]

James



More information about the Python-list mailing list