Home > Back-end >  How to convert alphabet to numbers?
How to convert alphabet to numbers?

Time:05-18

Trying to convert alphabet to numbers. An example of what I'm trying to do is convert the word "green" to a list of corresponding number index values: [6, 17, 4, 4, 13]

My eventual goal is to use it in a password encrypter thing, where I can convert the letters to numbers, do some math on the numbers so they're still on [0, 25], and then convert those numbers back to letters, so it's mixed up.

Here's code of what I've done so far:

def lettertonumber(word):
    flength = len(word)
    fcodera = "abcdefghijklmnopqrstuvwxyz"
    fcoderb = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
    fcoderlength = len(fcodera)
    numkey = []

    i = 0
    g = 0
    while i < flength:  # flength is the length of 'word' (in the case of 'green', 5)
        while g < fcoderlength:  # fcoderlength is the length of the alphabet (26)
            if word[i] == fcodera[g]:  # to convert it to the number
                numkey.append(fcoderb[g])  # fcoderb is a list with [0, 1, 2, 3... 24, 25] for the alphabet
            g = g   1
        i = i   1

    return numkey

word = 'green'
numberkey = lettertonumber(word)

The output of print(numberkey) is [6]. My question, then, is considering the loops that I have, why am I only getting one iteration of the i loop worth of numbers in numberkey[], when I should be getting as many as the length of the word (so that print(numberkey) outputs [6, 17, 4, 4, 13])

Sorry if my question is phrased poorly or unhelpful, this is my first question! Also sorry for my ignorance with Python -- I just learned it yesterday! Thank you!

CodePudding user response:

You can use the ord function to get the ascii value in decimal, then subtract 97. This can all be done within a list comprehension.

>>> word = 'green'
>>> [ord(l.lower()) - 97 for l in word]
[6, 17, 4, 4, 13]

If you wanted to go backwards, you can take the list of numbers and use the chr function to convert back to letters. Then join together with a blank string.

>>> numbers = [6, 17, 4, 4, 13]
>>> ''.join([chr(n   97) for n in numbers])
'green'

CodePudding user response:

I made changes to your code. Here it is:

def letterToNumber(word):
    numKey = []

    tmp = 0
    for i in word:
        tmp = i.lower()
        numKey.append(ord(tmp.lower()) - ord('a'))

    return numKey

word = 'green'
numberKey = letterToNumber(word)
print(numberKey)

Just renamed variable names to be more clear, and instead of finding index in alphabet string, i just used built-in ord() function which can convert character to ASCII code value. When I get ASCII code value for character I can convert it to index in alphabet by subtracting ord(character) and ord('a').

CodePudding user response:

Well I haven't really answered your question fully but here is a simple that function that converts numbers into letters.

def convert_to_alphabet(number_list):
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    alphabet_list = []
    for i in number_list:
        alphabet_list.append(alphabet[i])
    return alphabet_list

CodePudding user response:

i think this is what you searching for:

def lettertonumber(word):
    alphabet = []
    for i in range(97, 123):
        e = chr(i)
        alphabet.append(e)

    list_of_indexes = []
    for i in word:
        e = alphabet.index(i)
        list_of_indexes.append(e)

    print(list_of_indexes)


lettertonumber("green")
  • Related