How can the following be finished?
characters = [\'a\'\'b\'\'c\'\'d\'\'e\'\'f\'\'g\'\'h\'\'i\'\'j\'\'k\'\'l\'\'m\'\'n\'\'o\'\'p\'\'q\'\'r\'\'t\'\'u\'\'v\'\'w
def letter_to_int(letter):
alphabet = list('abcdefghijklmnopqrstuvwxyz')
return alphabet.index(letter) + 1
here, the index (x) function returns the position value of x if the list contains x.
Here is my solution for a problem where you want to convert all the letters in a string with position of those letters in English alphabet and return a string of those nos.
https://gist.github.com/bondnotanymore/e0f1dcaacfb782348e74fac8b224769e
Let me know if you want to understand it in detail.I have used the following concepts - List comprehension - Dictionary comprehension
You can map the alphabet to a list and return the index of each one as per the below :
import string
alphabet=string.ascii_lowercase
#alphabet='abcdefghijklmnopqrstuvwxyz'
#Get the character index , ex: e
print(chars.find('e'))
#This will return 4
If the goal is to transform only the letters abcd....xyz and ABCD....XYZ , I would use a function:
from string import letters
def rank(x, d = dict((letr,n%26+1) for n,letr in enumerate(letters[0:52]))):
return d[x]
I’ve written [0:52] because my Python 2.7 version displays the value
*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz*ƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ
for the string.letters argument.
Because the parameter d receives a value as a default argument, the calculus of this value is performed only once, at the moment when the definition of the function is executed to produce the function object. So, the function can then be used without this value to be calculated again, even if the function is appealed three thousand times.
By the way, lower() isn’t used again for each appeal of the function. The case of upper letters has been treated during the construction of the default argument.
.
One example of use:
word = 'supercalifragilisticexpialidocious'
print ''.join( letter if rank(letter)%3!=0 else '.' for letter in word)
result:
s.pe..a....ag...st..e.p.a..d.....s
.
It can be used with map() too :
print map(rank,'ImmunoElectroPhoresis')
result:
[9, 13, 13, 21, 14, 15, 5, 12, 5, 3, 20, 18, 15, 16, 8, 15, 18, 5, 19, 9, 19]
If you are just looking to map a number to a letter, then just do something simple like this:
def letter_to_index(letter):
_alphabet = 'abcdefghijklmnopqrstuvwxyz'
return next((i for i, _letter in enumerate(_alphabet) if _letter == letter), None)
Of course if you want to have it start at 1, then just add (i+1 for i, ... etc.
If you are looking the opposite like 1 = A , 2 = B etc, you can use the following code. Please note that I have gone only up to 2 levels as I had to convert divisions in a class to A, B, C etc.
loopvariable = 0
numberofdivisions = 53
while (loopvariable <numberofdivisions):
if(loopvariable<26):
print(chr(65+loopvariable))
loopvariable +=1
if(loopvariable > 26 and loopvariable <53):
print(chr(65)+chr(65+(loopvariable-27)))