20190120-自定义实现split方法
1. 实现字符串的split方法
Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串
思路同自定义实现replace方法类型:
1.先找出字符串中指定分隔字符的index,考虑可能出现多次的情况使用一个列表split_str_index存储分隔字符的index
2.使用result列表存储分隔后的字符串列表
3.当index不在split_str_index中的的时候拼接字符串,当index在split_str_index中的的时候的将已拼接的字符串append到result列表中,特别注意最后一定要判断each判断是否为空,来决定是否append一下
4.考虑分隔次数,使用count来统计分隔次数
def customize_split(s,split_str=' ',num=None):
result=[]
split_str_index=[]
for i in range(len(s)):
if s[i:i+len(split_str)]==split_str:
split_str_index.append(i)
#存储split_str的index
if num==None:
each =''
j=0
while j<len(s):
if j in split_str_index:
result.append(each)
each = ''
j+=len(split_str)
else:
each +=s[j]
j+=1
if bool(each):
print(bool(each))
result.append(each)
else:
each =''
j=0
count =0
while j<len(s):
if count<num and j in split_str_index:
if bool(each):
print(bool(each))
result.append(each)
each = ''
j+=len(split_str)
count+=1
else:
each +=s[j]
j+=1
if bool(each):
result.append(each)
#最后一根据each是否为空决定是否要append一下,因为有可能else是最后执行也可能if是最后执行
return result
print(customize_split('abcacabcacac','c'))
2019-02-17:新增对空白的切割,方法如下:
def customize_split(s,split_str=None,times=None):
i = 0
result =[]
item =''
if isinstance(s,str):
if times ==None and isinstance(split_str,str):
while i <len(s):
if s[i:i+len(split_str)]==split_str:
i+=len(split_str)
result.append(item)
#print(result)
item=''
else:
item +=s[i]
i+=1
result.append(item)
elif isinstance(times,int) and isinstance(split_str,str):
count =0
while i <len(s):
if s[i:i+len(split_str)]==split_str and count <times:
i+=len(split_str)
count+=1
result.append(item)
#print(result)
item=''
else:
item +=s[i]
i+=1
result.append(item)
elif times==None and split_str==None:
while i<len(s):
if (s[i]=='\t'or s[i]=='\r'or s[i]=='\n' or s[i]==' 'or s[i]=='\f'):
if item =='':
#print(item)
item=''
i+=1
else:
result.append(item)
item=''
i+=1
else:
item+=s[i]
i+=1
if item!='':
result.append(item)
elif isinstance(times,int) and isinstance(split_str,str):
while i<len(s):
if count<times and (s[i]=='\t'or s[i]=='\r'or s[i]=='\n'or s[i]==' 'or s[i]=='\f'):
if item =='':
#print(item)
item=''
i+=1
count+=1
else:
result.append(item)
item=''
i+=1
count+=1
else:
item+=s[i]
i+=1
if item!='':
result.append(item)
else:
result =-1
else:
result =-1
return result
print(customize_split(' a b\nc\td\re\f\t\r\ng'))