本文实例讲述了Python 26进制计算方法。分享给大家供大家参考。具体分析如下:
题目是这样的:
假设A=1,B=2,C=3...AA=27,AB=28...AAA=xxx(表示某个数字),写一个函数统计一个字符串的值是多少
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
27
28
29
30
|
# -*- coding:utf-8 -*- ''''' Created on 2013-3-29 @author: naughty ''' dict = {} dict [ 'A' ] = 1 dict [ 'B' ] = 2 dict [ 'C' ] = 3 dict [ 'D' ] = 4 dict [ 'E' ] = 5 dict [ 'F' ] = 6 dict [ 'G' ] = 7 dict [ 'H' ] = 8 dict [ 'I' ] = 9 dict [ 'J' ] = 10 dict [ 'K' ] = 11 dict [ 'L' ] = 12 dict [ 'M' ] = 13 dict [ 'N' ] = 14 # .....其他字母 def count_(s): p = len (s) - 1 count = 0 for x in xrange ( len (s)): count + = get(x) * dict [s[p - x]] return count def get(p): return 26 * * p print count_( 'AC' ) |
在上面的代码中,利用的字典来映射每个字母的含义。当然也可以每次计算每个字母代表的数字。但是会有效率问题。
希望本文所述对大家的Python程序设计有所帮助。