python在一个字符串中找到另外一个字符串并找到该字符起始的位置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaomifanhxx/article/details/83018927

代码详解

def index_of_str(seq, sub_seq):
    index=[]
    n1=len(seq)
    n2=len(sub_seq)
    for i in range(n1-n2+1):
        #print('seq==%s' % (seq[i:i + n2]))
        if seq[i:i+n2]==sub_seq:
            #print('seq==%s'%(seq[i:i+n2]))
            index.append(i+1)
    print(index)
index_of_str('ATGATAGAGGGATACGGGATAG', 'GATA')

def index_of_str(s1, s2):
    global dex
    dex=0
    index=[]
    lt=s1.split(s2)
    print(lt)
    num=len(lt)
    for i in range(num-1):
        dex+=len(lt[i])
        index.append(dex)
        dex+=len(s2)
    print(index)
    #if len(lt)==1:
        #return -1
    #return len(lt[0])
print(index_of_str('ATGATAGAGGGATACGGGATAG', 'GATA'))

猜你喜欢

转载自blog.csdn.net/xiaomifanhxx/article/details/83018927
今日推荐