A suggestion for a possible Python module

Andrew Dalke adalke at mindspring.com
Sat Mar 8 17:34:26 EST 2003


Lance LaDamage:
> > I have noticed that there is one thing that everyone wishes to do in
Python
> > ... reverse a string.

Mark VandeWettering:
> Honestly, what commonly programmed task requires string reversal?  I can't
> ever remember doing it even once during 20+ years of programming.

The ones I could think of are:
  - insert commas in a number, as in "10000" -> "10,000"

  def commafy(s):
    s = s.reverse()
    terms = []
    for i in range(0, len(s), 3):
      terms.append(s[i:i+3])
    return ",".join(terms).reverse()

  - for sequence analysis, compute the "reverse complement" of a sequence.
That's the sequence used for the other side of the DNA helix from the
current
sequence, eg, so that "aatccgatcg" -> "cgatcggatt"

  import string
  _rc_complement_trans = string.maketrans("atcg", "tacg")

  def rc(s):
    return s.reverse().translate(_rc_complement_trans)

I can think of a few other rare uses, but that's about it.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list