I am a beginner Python programmer. I am trying to turn each letter of the alphabet into their corresponding numbers (a=1, b=2...). I have tried using re.sub but since I can only substitute one character at a time, it requires 26 passes to translate just one character. Is there a better way to do this?
Asked
Active
Viewed 28 times
0
-
You might want to look up the function `ord`. – Scott Hunter Apr 30 '20 at 21:44
-
1May be `dict(zip(range(1,27),string.ascii_letters))`? – mad_ Apr 30 '20 at 21:46
-
`dict(enumerate(string.ascii_lowercase, 1))` - https://repl.it/@lobopt/PeriodicGuiltyMalware – Pedro Lobito Apr 30 '20 at 21:52
-
... or `','.join(x for x in map(lambda x: (ord(x) - 96) % 26), string))`. Tons of other ways, too. – Jongware Apr 30 '20 at 21:55