Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python For loop return Tuple--is there a better way to do this

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)
like image 361
Leerix Avatar asked Oct 21 '25 04:10

Leerix


1 Answers

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.

like image 121
Tim Pietzcker Avatar answered Oct 22 '25 17:10

Tim Pietzcker