在另一个函数Python

时间:2017-08-19 17:40:58

标签: python loops break

我正在尝试编写一个函数,它返回所有可能的k-mers,每个输入DNA字符串的最大汉明距离为d。 我最初的尝试是尝试直接迭代。

def motif_enumeration(k, d, DNA):
    pattern = []
    c = 0
    combos = combination(k)
    for combo in combos:
        for strings in DNA:
            while c+k < len(DNA[0])-1:
                if d >= hammingDistance(combo, strings[c:c+k]):
                    #move onto next string 
                elif d < hammingDistance(combo, string[c:c+k]) and c+k == len(DNA[0])-1:
                    #if the combo wasn't valid for a string, move onto the next combo
                    break
                elif d < hammingDistance(combo, string[c:c+k]):
                    #change the window to see if the combo is valid later in the string 
                    c += 1
        pattern+=[combo]   
    return pattern

不幸的是,我了解到我无法为外循环创建一个continue语句,并且我读到了解决这个问题的最佳方法是在循环中定义一个函数然后调用它。这对我来说非常困惑,但这是我尝试这种方法:

def motif_enumeration(k, d, DNA):
    combos = combination(k)
    global pattern
    global c
    for combo in combos:
        def inner(string, combo, count):
            global pattern
            global DNA
            global c
            if count==len(DNA):
                pattern += [combo]
                return pattern
            while c+k <= len(DNA[0])-1:
                if d >= hammingDistance(combo, string[c:c+k]):
                    #move onto next string 
                        count+=1
                        c = 0
                        string = DNA[count]
                        return inner(string, combo, count)
                elif d < hammingDistance(combo, string[c:c+k]) and c+k == len(DNA[0])-1:
                    #if the combo wasn't valid for a string, the inner function loop breaks
                    break
                elif d < hammingDistance(combo, string[c:c+k]):
                    #change the window to see if the combo is valid later in the string 
                    c += 1
        inner(DNA[0], combo, count = 0)
    return pattern

我的逻辑是我的内部函数将迭代我的列表DNA中的所有字符串,如果汉明距离小于或等于d,它将继续用一个新的DNA元素调用自己。然后,如果条件满足所有条件,因此count == len(DNA),那么模式将被更新并返回(因为它是一个全局变量,我不需要返回内部函数,只需调用它)。 然而,这并没有返回任何东西。任何建议的读数或只是直接建议,以帮助解决这个问题?

1 个答案:

答案 0 :(得分:1)

您不需要字面在另一个方法中定义一种方法。您只需要在第一个方法中调用inner方法。通过这种方式移动到外循环中的下一个迭代,您可以在return中调用inner_loop

def motif_enumeration(k, d, DNA):
    pattern = []
    c = 0
    combos = combination(k)
    for combo in combos:
        inner_loop(k,d,DNA,combo, c)
    return pattern

def inner_loop(k, d, DNA, combo, c):
    for strings in DNA:
        inner_loop_two(k, d, DNA, combo, c)

修改

要实现##move onto next string代码,您可以使用另一个内部函数。

您可以像这样分割inner_loop

def inner_loop(k, d, DNA, combo, c):
    for strings in DNA:
        inner_loop_two(k, d, DNA, combo, c)

def inner_loop_two(k, d, DNA, combo, c):
    while c+k < len(DNA[0])-1:
        if d >= hammingDistance(combo, strings[c:c+k]):
            #move onto next string 
            continue
        elif d < hammingDistance(combo, string[c:c+k]) and c+k == len(DNA[0])-1:
            #if the combo wasn't valid for a string, move onto the next combo
            break
        elif d < hammingDistance(combo, string[c:c+k]):
            #change the window to see if the combo is valid later in the string 
            c += 1
    pattern+=[combo]