-9

I have a dictionary of lists of integers. I want to build a new dictionary with the same keys, but where each number of each list is mapped to a letter in the alphabet. 0 is A, 1 is B, etc.

oldDict = {
    'code_1': [2, 0, 19],
    'code_2': [3, 14, 6],
    'code_3': [0, 11, 11],
    'code_4': [13, 0, 15]
}

This is what I want

newDict =  {
    'code_1': ['c', 'a', 't'],
    'code_2': ['d', 'o', 'g'],
    'code_3': ['a', 'l', 'l'],
    'code_4': ['n', 'a', 'p']
}

How would I be able to do this?

Xhattam
  • 195
  • 1
  • 11
tkxgoogle
  • 407
  • 4
  • 11
  • Did you mean: `'item_1': ['c', 'a', 't'],`? – 001 Mar 09 '20 at 17:53
  • 3
    Post your code, and we'll be happy to spot errors. I'd suggest dealing with each task separately. – Kenny Ostrom Mar 09 '20 at 17:53
  • do you have all the transformations you'd need to make already? – MattR Mar 09 '20 at 17:54
  • 2
    Already closed so can't flag duplicate, but [Convert numbers into corresponding letter using Python](https://stackoverflow.com/questions/23199733/convert-numbers-into-corresponding-letter-using-python/46399948) – G. Anderson Mar 09 '20 at 18:00
  • Does this answer your question? [Convert numbers into corresponding letter using Python](https://stackoverflow.com/questions/23199733/convert-numbers-into-corresponding-letter-using-python) – JonSG Dec 26 '21 at 22:00

2 Answers2

0

I hope this helps, but this post is a duplicate. Basically using chr() and passing it a number in ASCII we can get the letters the numbers correspond to. 'A' in ASCII is number 65 and 'a' is number 97. Also just need to use a nested loop for accessing that second list.

oldDict = {
    'code_1': [2, 0, 19],
    'code_2': [3, 14, 6],
    'code_3': [0, 11, 11],
    'code_4': [13, 0, 15]
}

# ASCII Implementation replace 97 - 'a' for 65 - 'A' for uppercase
count = 1
newDict = {}
for item in oldDict:
    newList = []
    for number in oldDict[item]:
        result = number + 97
        newList.append(chr(result))
    newDict['item_' + str(count)] = newList #dedent this line because you only have to updata when the newList finished computing
    count += 1

print(newDict)
greybeard
  • 2,249
  • 8
  • 30
  • 66
0

alternatively if you want to use any keys try:

oldDict = {
    'code_1': [2, 0, 19],
    'code_2': [3, 14, 6],
    'code_3': [0, 11, 11],
    'code_4': [13, 0, 15]
}

newDict = {}

for key, val in zip(oldDict, oldDict.values()):
    newDict[key] = []
    for num in val:
        newDict[key].append(chr(num+97))

print(newDict)
Julian wandhoven
  • 268
  • 2
  • 11