python实现每次处理一个字符的三种方法

yipeiwu_com5年前Python基础

本文实例讲述了python每次处理一个字符的三种方法。分享给大家供大家参考。

具体方法如下:

a_string = "abccdea" 
 
print 'the first' 
for c in a_string: 
  print ord(c)+1 
 
   
print "the second"   
result = [ord(c)+1 for c in a_string] 
print result 
 
print "the thrid" 
 
def do_something(c): 
  return ord(c)+1 
 
result = map(do_something ,a_string) 
print result 

打印出的结果如下:

the first 
98 
99 
100 
100 
101 
102 
98 
the second 
[98, 99, 100, 100, 101, 102, 98] 
the thrid 
[98, 99, 100, 100, 101, 102, 98] 

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python验证企业工商注册码

中国企业工商注册码前六位为行政区代码,中间8位顺序编码,最后一位为根据ISO 7064:1983.MOD 11-2校验码计算出来的检验码,本算法根据最后一位校验码的算法来判断企业注册码是...

Python3实现对列表按元组指定列进行排序的方法分析

本文实例讲述了Python3实现对列表按元组指定列进行排序的方法。分享给大家供大家参考,具体如下: Python版本: python3.+ 运行环境: Mac OS IDE: pyc...

python常见数制转换实例分析

本文实例讲述了python常见数制转换用法。分享给大家供大家参考。具体分析如下: 1.进位制度 Python中二进制是以0b开头的: 例如: 0b11 则表示十进制的3 8进制是以0开头...

python代码编写计算器小程序

本文实例为大家分享了python计算器小程序的具体代码,供大家参考,具体内容如下 import tkinter import tkinter.messagebox import ma...

Python实现迭代时使用索引的方法示例

本文实例讲述了Python实现迭代时使用索引的方法。分享给大家供大家参考,具体如下: 索引迭代 Python中,迭代永远是取出元素本身,而非元素的索引。 对于有序集合,元素确实是有索引的...