class Solution: def titleToNumber(self, s): """ :type s: str :rtype: int """ sum=0 dict1={'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'J':7,'H':8,'I':9,'G':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,} for i in range(0,len(s)): sum=sum+dict1[s[i]]26*(len(s)-i-1) return sum

t=Solution() print(t.titleToNumber('ZY'))

#第二种 sum = 0 for c in s: sum = sum*26 + ord(c) - 64 # 64 = ord('A') - 1 return sum

chr()函数用一个范围在range(256)内的(就是0~255)整数作参数,返回一个对应的字符。 unichr()跟它一样,只不过返回的是Unicode字符。 ord()函数是取出来一个字符对用的ASCII数值。