我正在嘗試從列表中的單詞中讀取每個字符,然後根據該單詞中的每個字母爲它們分配一個值。我的代碼是愚蠢長,我相信必須有一個更短的方式來做到這一點...更快地將數值分配給字母?
for c in tempWord:
if(c == "A"):
tempSum += 1
elif(c == "B"):
tempSum += 2
elif(c == "C"):
tempSum += 3
elif(c == "D"):
tempSum += 4
elif(c == "E"):
tempSum += 5
elif(c == "F"):
tempSum += 6
elif(c == "G"):
tempSum += 7
elif(c == "H"):
tempSum += 8
elif(c == "I"):
tempSum += 9
elif(c == "J"):
tempSum += 10
elif(c == "K"):
tempSum += 11
elif(c == "L"):
tempSum += 12
elif(c == "M"):
tempSum += 13
elif(c == "N"):
tempSum += 14
elif(c == "O"):
tempSum += 15
elif(c == "P"):
tempSum += 16
elif(c == "Q"):
tempSum += 17
elif(c == "R"):
tempSum += 18
elif(c == "S"):
tempSum += 19
elif(c == "T"):
tempSum += 20
elif(c == "U"):
tempSum += 21
elif(c == "V"):
tempSum += 22
elif(c == "W"):
tempSum += 23
elif(c == "X"):
tempSum += 24
elif(c == "Y"):
tempSum += 25
elif(c == "Z"):
tempSum += 26
這可能是一個愚蠢的問題,但我還是謝謝你!
下面的答案將完成這項工作。此外,此處添加信息:http://stackoverflow.com/questions/227459/ascii-value-of-a-character-in-python –