问题
You know, like A = 1, B = 2 etc. I could just make a long list of if-thens, but maybe there's already a module for it.
Bonus if it works like it does in "Excel Coordinates" where AA = 27 and continues. (Does this count as base26 numbers?)
回答1:
def foo(c):
return ord(c) - 64
foo('A')
1
foo('B')
2
off the top of my head :p
回答2:
from string import ascii_uppercase
letterKey = dict(list(zip( ascii_uppercase, range(1, 27))))
And as for the excel cords:
geExceltValue = lambda string: sum([26**i * list(reversed(map(lambda x: letterKey[x], string)))[i] for i in range(len(string))])
print geExceltValue("AA")
来源:https://stackoverflow.com/questions/29104338/code-golf-is-there-a-simple-way-to-convert-letters-to-numbers-in-python