Python 26进制计算实现方法

yipeiwu_com7年前Python基础

本文实例讲述了Python 26进制计算方法。分享给大家供大家参考。具体分析如下:

题目是这样的:

假设A=1,B=2,C=3...AA=27,AB=28...AAA=xxx(表示某个数字),写一个函数统计一个字符串的值是多少

# -*- 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程序设计有所帮助。

相关文章

Python中使用ConfigParser解析ini配置文件实例

ini文件是windows中经常使用的配置文件,主要的格式为: 复制代码 代码如下: [Section1] option1 : value1 option2 : value2 pyth...

Python 获取新浪微博的最新公共微博实例分享

API: statuses/public_timeline  返回最新的200条公共微博,返回结果非完全实时 CODE: #!/usr/bin/python # -*-...

Python+request+unittest实现接口测试框架集成实例

Python+request+unittest实现接口测试框架集成实例

1、为什么要写代码实现接口自动化 大家知道很多接口测试工具可以实现对接口的测试,如postman、jmeter、fiddler等等,而且使用方便,那么为什么还要写代码实现接口自动化呢?工...

selenium python浏览器多窗口处理代码示例

本文主要研究的是selenium python浏览器多窗口处理的相关内容,分享了操作实例代码,具体如下: #!/usr/bin/python # -*- coding: utf-8...

Python线程障碍对象Barrier原理详解

python线程Barrier俗称障碍对象,也称栅栏,也叫屏障。 一.线程障碍对象Barrier简介 # 导入线程模块 import threading # 障碍对象barrier...