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命令行click参数用法解析

这篇文章主要介绍了Python命令行click参数用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一、前言 在概念上, cl...

Python实现识别手写数字 简易图片存储管理系统

Python实现识别手写数字 简易图片存储管理系统

写在前面 上一篇文章Python实现识别手写数字—图像的处理中我们讲了图片的处理,将图片经过剪裁,拉伸等操作以后将每一个图片变成了1x10000大小的向量。但是如果只是这样的话,我们每一...

详解pyenv下使用python matplotlib模块的问题解决

详解pyenv下使用python matplotlib模块的问题解决

先来描述一下我遇到的问题,在进行matplotlib学习时, plot.show() 总是无法成功运行,总是会报一个错: RuntimeError: Python is not ins...

在python中使用requests 模拟浏览器发送请求数据的方法

如下所示: import requests url='http://####' proxy={'http':'http://####:80'} headers={ "Accep...

pytorch中torch.max和Tensor.view函数用法详解

torch.max() 1. torch.max()简单来说是返回一个tensor中的最大值。 例如: >>> si=torch.randn(4,5) >&g...