testing for exact match of key strings in target strings. output must be tuple of starting points of matches. My code works but i feel like it can be much much neater. How could i return a tuple without converting from an appended list? searched everywhere and can't seem to find an answer. Thanks!!
from string import *
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'
def subStringMatchExact(target, key):
match_list = []
location = 0
for i in target:
ans = find(target, key, location)
if ans >= 0:
match_list.append(ans)
location = ans + (len(key))
print tuple(match_list)
subStringMatchExact(target1, key11)
This is a perfect job for regular expressions.
import re
def subStringMatchExact(target, key):
regex = re.compile(re.escape(key))
return tuple(match.start() for match in regex.finditer(target))
Note that this finds non-overlapping matches only. If you want to find overlapping matches, too:
def subStringMatchExact(target, key):
regex = re.compile("(?=" + re.escape(key) + ")")
return tuple(match.start() for match in regex.finditer(target))
Of course, unless you actually need the result to be a tuple, you could just remove the tuple
from the last line and have your function return a more efficient generator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With