newby question: Splitting a string - separator
Fredrik Lundh
fredrik at pythonware.com
Sat Dec 10 04:31:30 EST 2005
James Stroud wrote:
> >> 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.
>
> Just an example. Here is the "correct version":
>
> names = [n for n in data.split(" ") if n]
where "correct" is "still wrong", and "theoretically faster" means "slightly
slower" (at least if fix your version, and precompile the pattern).
</F>
More information about the Python-list
mailing list