1. Realize the split method of string
Python split() slices strings by specifying a separator. If the parameter num has a specified value, it separates num+1 substrings
The idea is the same as the custom implementation of replace method type:
1. First find the index of the specified separator character in the string, and use a list split ﹣ str ﹣ index to store the index of the separator character in consideration of the possibility of multiple occurrences
2. Use result list to store separated string list
3. When the index is not in split STR index, concatenate the string. When the index is in split STR index, append the concatenated string to the result list. Pay special attention to whether the final judgment of each is empty to decide whether to append
4. Consider the number of separations, and use count to count the number of separations
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)
#storage split_str Of 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)
#Last basis each Whether it is empty determines whether to append Just a moment, because it's possible else Last execution possible if It's the last execution
return result
print(customize_split('abcacabcacac','c'))