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

相关文章

Django中Middleware中的函数详解

一个middleware的例子 import time from django.urls import reverse from django.utils.deprecation...

Python调用.NET库的方法步骤

开发背景是这样的:整个项目中使用很多台摩托罗拉的RFID读卡器,我要为这些读卡器写一个管理程序,判断是否有RFID标签进入或离开某个区域。用户提供给我的,除了设备,就是一个.net的动态...

Django+JS 实现点击头像即可更改头像的方法示例

Django+JS 实现点击头像即可更改头像的方法示例

首先,在models.py中创建UserModels类 from django.db import models from django.contrib.auth.models im...

一键搞定python连接mysql驱动有关问题(windows版本)

对于mysql驱动问题折腾了一下午,现共享出解决方案 1:手动安装驱动 完全是场噩梦,推荐大家采用自动安装 2:自动安装 下载自动安装包,下载地址:https://www.jb51.ne...

python中open函数的基本用法示例

前言 本文主要介绍的是关于python中open函数用法的相关资料,用法如下: name = open('errname.txt','w')<br>name.readli...