0

I cant seem to figure out how to scan through my dictionary to find the characters in cometList and then append the numbers to my numList

i.e: I input comet and cometList becomes (C, O, M, E, T) it would then scan alphabetList and get the corresponding numbers (3, 15, 13, 5, 20) and append them to numList

alphabetList = {'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': '26'}
cometList = list(comet)
groupList = list(group)
numList =[]
  • Look at [this question](http://stackoverflow.com/questions/4528982/convert-alphabet-letters-to-number-in-python). It might give you a better way to handle things. – Ffisegydd Apr 08 '14 at 20:38

2 Answers2

2
word = "comet"
codes = [alphabet[letter] for letter in word.upper()] 

You don't need a list of letters - just iterate the word directly.

gog
  • 10,367
  • 2
  • 24
  • 38
  • Oh wow that was much simpler then expected, however now when i'm trying to multiply the codes using "numComet = reduce(operator.mul, codes)" I am getting 'can't multiply sequence by non-int of type 'str' – user3512662 Apr 08 '14 at 20:51
  • @user3512662 If you want to multiply, the elements of `codes` list must be integers. Do this `codes = [int(alphabet[letter]) for letter in word.upper()]` – shaktimaan Apr 08 '14 at 20:59
  • @shaktimaan thank you :) fixed all my errors for this problem . – user3512662 Apr 08 '14 at 21:05
0

This is what you are looking for:

cometList = 'comet'
numList = [alphabetList[letter.upper()] for letter in cometList]
shaktimaan
  • 11,962
  • 2
  • 29
  • 33