Search code examples
stringpython-2.7comparealphabet

Python get character position matches between 2 strings


I'm looking to encode text using a custom alphabet, while I have a decoder for such a thing, I'm finding encoding more difficult.

Attempted string.find, string.index, itertools and several loop attempts. I would like to take the position, convert it to integers to add to a list. I know its something simple I'm overlooking, and all of these options will probably yield a way for me to get the desired results, I'm just hitting a roadblock for some reason.

alphabet = '''h8*jklmnbYw99iqplnou b'''

toencode = 'You win'

I would like the outcome to append to a list with the integer position of the match between the 2 string. I imagine the output to look similar to this:

[9,18,19,20,10,13,17]


Solution

  • Ok, I just tried a bit harder and got this working. For anyone who ever wants to reference this, I did the following:

    newlist = []
    for p in enumerate(flagtext):
        for x in enumerate(alphabet):
            if p[1] == x[1]:
                newlist.append(x[0])
    

    print newlist