在另一个函数中定义一个函数的逻辑 Python

Logic of Defining a Function inside another Function Python

我正在尝试编写一个函数,该函数 return 包含与每个输入 DNA 字符串的最大汉明距离为 d 的所有可能的 k 聚体。 我最初的尝试是直接尝试迭代。

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),那么模式将被更新并 returned(因为它是一个全局变量,我不需要 return 内部函数,直接调用即可)。 但是,这不会 return 任何东西。有任何建议的读物或直接的建议来帮助解决这个问题吗?

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

中调用 return
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]