我正在上一門生物資訊學課程,我正在嘗試撰寫一個函式來查找字串中所有出現的子字串。
def find_match(s, t):
"""Returns a list of all positions of a substring t in string s.
Takes two arguments: s & t.
"""
occurrences = []
for i in range(len(s)-len(t) 1): # loop over alignment
match = True
for j in range(len(t)): # loop over characters
if s[i j] != t[j]: # compare characters
match = False # mismatch
break
if match: # allchars matched
occurrences.append(i)
return(occurrences)
print(find_match("GATATATGCATATACTT", "ATAT")) # [1, 1, 1, 1, 3, 3, 3, 3, 5, 5, 9, 9, 9, 9, 11, 11, 11, 13]
print(find_match("AUGCUUCAGAAAGGUCUUACG", "U")) # [1, 4, 5, 14, 16, 17]
上面的輸出應該與以下內容完全匹配:
[2, 4, 10]
[2、5、6、15、17、18]
我怎樣才能解決這個問題?最好不使用正則運算式。
uj5u.com熱心網友回復:
看起來你的代碼縮進很嚴重,
if match:
必須在內回圈之外。
def find_match(s, t):
"""Returns a list of all positions of a substring t in string s.
Takes two arguments: s & t.
"""
occurrences = []
for i in range(len(s)-len(t) 1): # loop over alignment
match = True
for j in range(len(t)): # loop over characters
if s[i j] != t[j]: # compare characters
match = False # mismatch
break
if match: # <--- This shouldn't be inside the inner for cycle
occurrences.append(i 1)
return occurrences
print(find_match("GATATATGCATATACTT", "ATAT")) # [1, 1, 1, 1, 3, 3, 3, 3, 5, 5, 9, 9, 9, 9, 11, 11, 11, 13]
print(find_match("AUGCUUCAGAAAGGUCUUACG", "U")) # [1, 4, 5, 14, 16, 17]
uj5u.com熱心網友回復:
你可以用find,
def find_match(s, t):
return list(set([s.find(t, i) 1 for i in range(len(s)-1) if s.find(t, i) != -1]))
輸出:
In [1]: find_match("AUGCUUCAGAAAGGUCUUACG", "U")
Out[1]: [2, 5, 6, 15, 17, 18]
In [2]: find_match("GATATATGCATATACTT", 'ATAT')
Out[2]: [2, 10, 4]
Find 將回傳子字串的位置。所以遍歷索引并將其傳遞給str.find方法。如果子字串不存在find將回傳-1。所以需要過濾掉。
In [1]: "GATATATGCATATACTT".find('ATAT', 0)
Out[1]: 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/516512.html
上一篇:Java檢查多個字串是否為空
下一篇:在兩個單詞后分組子字串
