这个代码应该读取一个基因组的文本文件,并且给定一个模式,应该返回该模式出现的次数及其位置。相反,它只返回出现的次数和第一次出现的位置。这是一个运行代码的示例,它不返回35次出现的位置,而是返回第一个位置35次。
# open the file with the original sequence
myfile = open('Vibrio_cholerae.txt')
# set the file to the variable Text to read and scan
Text = myfile.read()
# insert the pattern
Pattern = "TAATGGCT"
PatternLocations = []
def PatternCount(Text,Pattern):
count = 0
for i in range (len(Text)-len(Pattern)+1):
if Text [i:i+len(Pattern)] == Pattern:
count +=1
PatternLocations.append(Text.index(Pattern))
return count
# print the result of calling PatternCount on Text and Pattern.
print (f"Number of times the Pattern is repeated: {PatternCount(Text,Pattern)} time(s).")
print(f"List of Pattern locations: {PatternLocations}")
2条答案
按热度按时间ctrmrzij1#
是的
Return the lowest index in S where substring sub is found
PatternLocations.append(i)
if Text [i:i+len(Pattern)] == Pattern:
nmpmafwu2#
我建议您使用
re
.下面是一个片段:
来自我使用的自定义示例(
pattern='livraison'
)它返回的结果是这样的: