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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

tensorflow学习笔记之mnist的卷积神经网络实例

mnist的卷积神经网络例子和上一篇博文中的神经网络例子大部分是相同的。但是CNN层数要多一些,网络模型需要自己来构建。 程序比较复杂,我就分成几个部分来叙述。 首先,下载并加载数据:...

Python封装原理与实现方法详解

本文实例讲述了Python封装原理与实现方法。分享给大家供大家参考,具体如下: 【封装】  隐藏对象的属性和实现细节,仅对外提供公共访问方式。 【好处】 1. 将变化隔离; 2...

python类继承用法实例分析

本文实例讲述了python类继承用法。分享给大家供大家参考。具体如下: help('object') # test class Class1(object): """ Cla...

python读取dicom图像示例(SimpleITK和dicom包实现)

1. 用SimpleITK读取dicom序列: import SimpleITK as sitk import numpy as np img_path='F:\\dataset\\...

Flask之请求钩子的实现

请求钩子 通过装饰器为一个模块添加请求钩子, 对当前模块的请求进行额外的处理. 比如权限验证. 说白了,就是在执行视图函数前后你可以进行一些处理,Flask使用装饰器为我们提供了注册...