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

相关文章

python语言线程标准库threading.local解读总结

本段源码可以学习的地方: 1. 考虑到效率问题,可以通过上下文的机制,在属性被访问的时候临时构建; 2. 可以重写一些魔术方法,比如 __new__ 方法,在调用 object.__ne...

Windows下Python的Django框架环境部署及应用编写入门

环境搭建 1、下载所需的软件包: (1)python安装包 (2)django安装包 以下2个包其实是安装python包管理工具,在后面安装django文档包模块时会用到,下载网站是py...

Python获取Linux系统下的本机IP地址代码分享

有时候使用到获取本机IP,就采用以下方式进行。 复制代码 代码如下: #!/usr/bin/python   import socket import struct impor...

对django xadmin自定义菜单的实例详解

1、 自定义菜单 adminx.py class GlobalSetting(object): site_title = u'xxx后台' def kuF_site_menu...

Pytoch之torchvision.transforms图像变换实例

transforms.CenterCrop(size) 将给定的PIL.Image进行中心切割,得到给定的size,size可以是tuple,(target_height, target...