实现Python str.split功能的小函数

s="""    tt qq xbc wzx  ydc  eeff   mm
asdfa sd s s    ssss   ss
sss  sa   aa   aa   q qq
sd              d              """
lis = s.split()
for k in lis:
    print k
i=0
tem = ""
for t in s:
    if t!=" ":
        tem = tem + t
        if i+1 < len(s):
            if s[i+1]==" ":
                print tem
                tem=""
        else:
            print tem
    i=i+1

以上为以空格为分隔符,以下为以'..'为分隔符。

s="s...s.....d......r....e..."
lis = s.split('..')
print lis
i=0
k=0
tem = ""
for t in s:
    if t!=".":
        tem = tem + t
        j = i 
        if i+1 < len(s):
            if s[i+1]==".":
                print tem
                tem=""
        else:
            print tem
    else:
        if i+1 < len(s):        
            k=i
            if s[k+1]!="." and (k-j)%2==0:
                print k,j
            if s[k+1]!="." and (k-j)%2!=0:
                print "Cant not !"
        else:
            if (k-j+1)%2!=0:
                print j,k
                print "Can not !"
            
    i=i+1

自定义函数 my_split 实现系统自带split的函数

def my_split (src,sep):
    a= src.find("..")
    w1=src[:a]
    print w1
    b=a
    i=0
    while b<=len (src) and b != -1:
        while src[a:a+len(sep)] == sep:
            a=a+len(sep)
        b=src.find(sep,a)
        if b != -1:
            print src[a:b]
        else :
            print src [a:]
        a=b
        i=i+1
my_split ("wwwww..wwww..wwww..www",".")
def my_split (src,sep):
    b= src.find (sep)
    e= src.find (sep)
    if b== 0:
        print "\'\'"
    else:
        print src [:e]
    while e !=-1:
        b=src.find (sep,e)
        e=src.find(sep,b+len(sep))
        if e-b!=len(sep) and e-b!=-1 :
            print src [b+len(sep):e]
        elif e==-1:
            print src[b:]
        else:
            print "\'\'"

s="aaa...a..a.....a...addd"
my_split(s,'..')



你可能感兴趣的:(Python)