Handling backspace chars in a string...

Tim Peters tim_one at email.msn.com
Sun Apr 25 22:26:05 EDT 1999


[Purple]
> I didn't post the code for that bit [newlines] as it seems easy enough
> to take care of processing those with something like
> map(string.strip,string.split(stringWithNewlines,"\n")
>
> Unless there's a better way to do that too? :)

I don't know what you're trying to do.  What that code *does* is break the
string into chunks as separated by newlines, strips leading and trailing
whitespace of all kinds from each resulting chunk, and leaves the result as
a list of strings.  That doesn't sound like what you *wanted* to do, but if
it is I can't think of a better to do that <wink>.

> ...
> [code using lists snipped]
> I may well go that route... Is it any slower or faster to do this
> using lists rather than counting up the backspaces and slicing around
> the bits that need to be snipped?

from time import clock

How big are the strings?  What's the expected distribution of backspaces?
Etc.  The list code is worst-case linear-time (in practice although not in
theory); the string slicing worst-case quadratic (although "acts linear"
until strings reach a platform-dependent size).  There's no definitive
answer to which is faster without a strong characterization of your data.
Don't bother telling me, time it <wink>.

in-the-end-is-it-faster-to-run-or-walk?-yes-ly y'rs  - tim






More information about the Python-list mailing list