split()函数实现
#split函数实现: ss='** *axx* *bv** *ctt** **dff***' result=[] def split_1(ss,a,times=len(ss)): i=0 n=0 while len(ss)!=0 and i<len(ss)and n<times: if ss[i:i+len(a)]==a: print('i:',i) print('之前的ss:',ss) result.append(ss[0:i]) print(result) ss=ss[i+len(a):] n+=1 #n是分割次数,每分割一次记录+1 i=0 #ss重新赋值了*后面的数据,i需要重新初始化为0 print('之后的ss:',ss) else: i+=1 print('最后的i:',i) else: result.append(ss) return result print(split_1(ss,' ',100)) #split 处理以空白分割的情况 ss= 'abc \t \t \n\r bcf \n\rff' aa='' #临时存储非空白字符串 result=[] i=0 while i<len(ss): print('内层i:',i) if ss[i] not in '\t\r\n ': aa+=ss[i] print('aa:',aa) else: if aa not in result and aa!='': result.append(aa) aa='' print('内层result:',result) i+=1 result.append(aa) print(result)