Python字符遍历的艺术
时间:2021-09-29 08:48:29|栏目:Python代码|点击: 次
比如,将一个字符串转换为一个字符数组:
theList = list(theString)
同时,我们可以方便的通过for语句进行遍历:
for c in theString:
do_something_with(c)
甚者,使用这样的语句:
result = [do_something_with(c) for c in theString if c == 'x']
同时,还可以使用map语句,下面,我们开始上菜吧!传说中有一个神奇的字符串,被病毒感染了,被病毒附上了许多x字符,你将设计一个引擎,把病毒x出去,把我们神奇的字符串输出来。程序如下:
theString = 'Ix lixkxex xpxytxhxonx !'
def PrintEngine(c):
if c != 'x':
print c,
map(PrintEngine, theString)
输出结果:
I like python !
theList = list(theString)
同时,我们可以方便的通过for语句进行遍历:
for c in theString:
do_something_with(c)
甚者,使用这样的语句:
result = [do_something_with(c) for c in theString if c == 'x']
同时,还可以使用map语句,下面,我们开始上菜吧!传说中有一个神奇的字符串,被病毒感染了,被病毒附上了许多x字符,你将设计一个引擎,把病毒x出去,把我们神奇的字符串输出来。程序如下:
theString = 'Ix lixkxex xpxytxhxonx !'
def PrintEngine(c):
if c != 'x':
print c,
map(PrintEngine, theString)
输出结果:
I like python !
上一篇:python增加图像对比度的方法
栏 目:Python代码
本文标题:Python字符遍历的艺术
本文地址:http://www.codeinn.net/misctech/177992.html






