在Python中字符就是长度为1的字符串,所以可以循环遍历一个字符串,依次访问每一个字符,得到你想要的处理前提: 一个列表是个好主意,就像这样:thelist = list(thestring) 当然,完全可以不用列表,对于喜欢循环遍历的人,他们有足够的理由这么做,因为并没有创建列表的过程: for c in thestring: do_something_with(c) 知道列表推导的人,肯定不屑于上面的写法,因为下面的代码是他们常引以为豪的: results = [do_something_…
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…
""" Python3.4[文本]之每次处理一个字符 """ test_str = "my name is bixiaopeng" for x in range( 0, len(test_str)-1): print ("## 通过索引遍历字符串: " + test_str[x]) for x in test_str: print ("## 直接遍历字符串: " + x) thelist…
1.给出任意一个字符串,打印一个最长子串字符串及其长度,如果有相同长度的子字符串,都要一起打印出来,该子字符串满足以下条件, 第一个字母和最后一个字符是第一次重复 这个子字符串的中间字母没有重复 这个子字符串是满足条件里面的最长的 如: adsasadmasd 中满足条件的是dmasd import re def maxsubstring(s): res_list=[] max_len=0 for i in range(len(s)): index=s[i+1:].find(s[i]) if i…