Search code examples
pythonloopsmathpython-itertoolsrecycle

Converting letters to numbers and performing sequence of operations along the length in Python


What has been done already:

  1. Each letter of a string comprised of fixed number of letters converted to a number: e.g. "TAAPAS" is now "122324"

    I was forced to perform this using the clunky code:

    s = s1.replace("A", "2").replace("P", "3").replace("T", "1").replace("S", "4") 
    

because when I tried a more standard approach suggestion on an earlier forum, the code failed to modify every letter:

numerize = {'A':'2', 'P':'3', 'T':'1', 'S':'4'}
for k, v in numerize.iteritems():
    string2 = string1.replace(k, v)

Q1: Any suggestions on why this code isn't working in my situation? I even tried the following, with the same result:

numerize = {'A':'2', 'P':'3', 'T':'1', 'S':'4'}
for k, v in numerize.iteritems():
    for i in string1: 
         string2 = string1.replace(k, v)

Anyhow, on to what I want to do next:

  1. On my sequence of numbers "122324", I want to perform a defined series of numerical operations, and when the last operation is reached, I want it to start over at the beginning...repeating this as many times as necessary to reach the end of my integer.

    e.g. +, *, /

    such that it looks something like:

     y = 1 + 2 * 2 / 3 + 2 * 4 ...
    

Q2: Must this be performed on a list of numbers? or is it possible to iterate through the positions of an integer as one can the positions of a string. If not, it's not a big deal to just split my string prior to converting it.

Q3. What is the code to accomplish such a task? And is there a name for this type of procedure? Using the keywords "recycle" and "math functions" leaves me dry...and it seems that the itertools library doesn't do math (or so there are no examples on the webpage).

Thank you!


Solution

  • All the given answers (@6:30 GMT) work for just one string.

    If you have to encode many strings with the provision that, for each string, you assign the code 1 to the first letter in the string, 2 to the second different letter in the input string etc you can give a try to the following function.

    def numerize(s):
        nd = 0
        copy = s[::]
        for i  in range(len(s)):
            c = copy[i]
            if c.isdigit(): continue
            nd = nd + 1
            if nd > 9 :
                print "Too many different characters in \"%s\"." % s
                return None # here you may want to throw
            copy = copy.replace(c,repr(nd))
        return copy
    
    print numerize('asdeaswes')
    

    A tacit assumption, that I want to speak out, is that the input string doesn't contain any digit. You may want to check each character before processing if you need, e.g., that the only legal characters are latin capital letters A to Z.

    ps: if you need a list of integers in place of a string of digits,

     s/return copy/return [int(d) for d in copy]/
    

    pps: concerning the numerical post-processing, imo it's way better to follow J.F. Sebastian's advice

    eval_expr(interlace(numerize("TAAPAS"), cycle("+*/")))
    

    rather than reinventing wheel on your own...