String manipulation in python..NEED HELP!!!!
rridge at csclub.uwaterloo.ca
Tue Dec 11 21:52:30 CET 2012
John Gordon wrote:
> def encode(plain):
> '''Return a substituted version of the plain text.'''
> encoded = ''
> for ch in plain:
> encoded += key[alpha.index(ch)]
> return encoded
Terry Reedy <tjreedy at udel.edu> wrote:
>The turns an O(n) problem into a slow O(n*n) solution. Much better to
>build a list of chars and then join them.
There have been much better suggestions in this thread, but John Gordon's
code above is faster than the equivilent list and join implementation
with Python 2.6 and Python 3.1 (the newest versions I have handy).
CPython optimized this case of string concatenation into O(n) back in
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rridge at csclub.uwaterloo.ca
More information about the Python-list