简体   繁体   中英

Using a dict to translate numbers to letters in python

I have a string 'alphabet' with all the letters in the alphabet and a list of ints corresponding to those those letters (0-25).

Ex:

num_list = [5,3,1] would translate into letter_list = ['f','d','b']

I currently can translate with:

letter_list = [alphabet[a] for a in num_list]

However, I would like to use a dict for the same thing, retrieve 'letter' keys from dict with 'number' values.

alpha_dict = {'a':0,'b':1,'c':2}... etc

How do I change my statement to do this?

Simply iterate through your alphabet string, and use a dictionary comprehension to create your dictionary

# Use a dictionary comprehension to create your dictionary
alpha_dict = {letter:idx for idx, letter in enumerate(alphabet)}

You can then retrieve the corresponding number to any letter using alpha_dict[letter] , changing letter to be to whichever letter you want.

Then, if you want a list of letters corresponding to your num_list , you can do this:

[letter for letter, num in alpha_dict.items() if num in num_list]

which essentially says: for each key-value pair in my dictionary, put the key (ie the letter) in a list if the value (ie the number) is in num_list

This would return ['b', 'd', 'f'] for the num_list you provided

You could also invert alpha_dict to do this:

>>> import string
>>> alpha_dict = dict(enumerate(string.ascii_lowercase))
>>> alpha_dict
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r', 18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z'}
>>> numbers = [5, 3, 1]
>>> [alpha_dict[x] for x in numbers]
['f', 'd', 'b']

This way your dictionary maps numbers to letters, instead of letters to numbers.

You can build a dict of letter/numbers as such:

import string

alpha_dict = {
    i: string.ascii_lowercase[i]
        for i in list(range(0, len(string.ascii_lowercase)))
}

num_list = [5,3,1]
letter_list = [alpha_dict.get(a) for a in num_list]

However it would be easier just to use the list like this:

import string

num_list = [5,3,1]
letter_list = [string.ascii_lowercase[a] for a in num_list]

I think -- like the other answers -- that a list should be used (to utilize the handy index calling) but dictionaries should NOT be used, period. SO...

    from string import ascii_lowercase # ONLY import ascii_lowercase
    numList = [numbers, in, your, list] # like you said in your question
    translation = "".join ([ascii_lowercase[num] for num in numList])
letters = [chr(i) for i in range(97, 123)]
alph_index = dict(zip(letters, range(26)))
    
print(">> letters=", letters)
print(">> alph_index=", alph_index)
  • ord('a') = 97
  • chr(97) = 'a'
  • 97 is a, 65 is A, 122 is z
Output:
>> letters= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

>> alph_index= {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM