def get_indices_from_the_second_string(string1, string2):
'''(str, str) -> list of int
>>> get_indices_from_the_second_string('AGTACACGTTAC', 'GAATTC')
[1, 3, 5, 8, 9, 11]
>>> get_indices_from_the_second_string('AGTACACGTTAC', 'GGATCC')
[1, 7, 10]
'''
acc= []
for i in range(0, len(string2)):
for r in range(0, len(string1)):
if len(acc) == len(string2):
break
if string1[r] == string2[i]:
acc.append(r)
i += 1
r += 1
return acc
# the second example is wrong
# how to make it not reversed only from left to right
# maybe use find.()???
解决方法:
def get_indices_from_the_second_string(string1, string2):
acc = []
s2_counter = 0
for i, letter in enumerate(string1):
if letter == string2[s2_counter]:
acc.append(i)
s2_counter += 1
if len(acc) == len(string2):
break
return acc
a = get_indices_from_the_second_string('GAATTCCGTTAC', 'GAATTC')
标签:python
来源: https://codeday.me/bug/20191012/1903886.html