alternating string replace

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Jan 11 05:46:10 EST 2008


Dennis Lee Bieber:
> So� in Python, your str[n] :=
> ':' just can not be done! You would have to create a new string
> containing everything in front of n, the ':', and then everything behind
> n (skipping n itself, of course). This is a painfully slow operation in
> Python as it allocates memory for the parts, allocates memory for the
> new combined string, and then garbage collects (potentially) the
> original string and parts. The recommended Python idiom is to break
> strings into a list of separate parts (str.split('_'), for example),
> manipulate those parts, and at the end of manipulation, rejoin them
> using some delimiter ( ",".join(partslist) )

An alternative solution, that's often good, expecially wity Psyco, is
to use an:
array.array("c", originalstring)
and then mutate it, as you do with Pascal strings.

Bye,
bearophile



More information about the Python-list mailing list