【python】字符遍历,,Python为我们提


Python为我们提供了很多便捷的方式去遍历一个字符串中的字符。比如,将一个字符串转换为一个字符数组(列表):

theList=list(theString)

同时,我们可以方便的通过for语句进行遍历:

bubuko.com,布布扣for c in theString:
do_something_with(c)

map函数用法:

第一个参数接收一个函数名,第二个参数接收一个可迭代对象

lt = [1, 2, 3, 4, 5, 6]def add(num):    return num + 1 rs = map(add, lt)print rs #[2,3,4,5,6,7]
theString = ‘Ix lixkxex xpxytxhxonx !‘def PrintEngine(c):    if c != ‘x‘:        print c,map(PrintEngine, theString)

输出结果:

I like python !

【python】字符遍历,布布扣,bubuko.com

【python】字符遍历

评论关闭