python在一個字串中找到另外一個字串並找到該字元起始的位置
阿新 • • 發佈:2018-12-14
程式碼詳解
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'))