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
Here's something I use to convert excel column letters to numbers (so a limit of 3 letters but it's pretty easy to extend this out if you need more). Probably not the best way but it works for what I need it for.
def letter_to_number(letters):
letters = letters.lower()
dictionary = {'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}
strlen = len(letters)
if strlen == 1:
number = dictionary[letters]
elif strlen == 2:
first_letter = letters[0]
first_number = dictionary[first_letter]
second_letter = letters[1]
second_number = dictionary[second_letter]
number = (first_number * 26) + second_number
elif strlen == 3:
first_letter = letters[0]
first_number = dictionary[first_letter]
second_letter = letters[1]
second_number = dictionary[second_letter]
third_letter = letters[2]
third_number = dictionary[third_letter]
number = (first_number * 26 * 26) + (second_number * 26) + third_number
return number
Use this function. It converts a string of alphabet to its equivalent digit value:
def convAlph2Num(sent):
alphArray = list(string.ascii_lowercase)
alphSet = set(alphArray)
sentArray = list(sent.lower())
x = []
for u in sentArray:
if u in alphSet:
u = alphArray.index(u) + 1
x.append(u)
print(x)
return
If you are going to use this conversion a lot, consider calculating once and putting the results in a dictionary:
>>> import string
>>> di=dict(zip(string.letters,[ord(c)%32 for c in string.letters]))
>>> di['c']
3
The advantage is dictionary lookups are very fast vs iterating over a list on every call.
>>> for c in sorted(di.keys()):
>>> print "{0}:{1} ".format(c, di[c])
# what you would expect....
import string
# Amin
my_name = str(input("Enter a your name: "))
numbers = []
characters = []
output = []
for x, y in zip(range(1, 27), string.ascii_lowercase):
numbers.append(x)
characters.append(y)
print(numbers)
print(characters)
print("----------------------------------------------------------------------")
input = my_name
input = input.lower()
for character in input:
number = ord(character) - 96
output.append(number)
print(output)
print("----------------------------------------------------------------------")
sum = 0
lent_out = len(output)
for i in range(0,lent_out):
sum = sum + output[i]
print("resulat sum is : ")
print("-----------------")
print(sum)
resualt is :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
['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']
----------------------------------------------------------------------
[1, 13, 9, 14]
----------------------------------------------------------------------
resulat sum is :
-----------------
37