Python 递归函数中的两个函数?

Python 递归函数中的两个函数?,python,list,function,recursion,Python,List,Function,Recursion,我几乎完成了一个特定程序的编码。我只需要最后一件东西的帮助。 程序接受一个单词并将一个字母更改为更接近目标单词。 更改后的单词必须存在于给我的词典中 我的职能是: def changeling(word,target,steps): holderlist=[] i=0 if steps==0 and word!=target: return None return holderlist if len(word)!=len(targ

我几乎完成了一个特定程序的编码。我只需要最后一件东西的帮助。 程序接受一个单词并将一个字母更改为更接近目标单词。 更改后的单词必须存在于给我的词典中

我的职能是:

def changeling(word,target,steps):
    holderlist=[]
    i=0
    if steps==0 and word!=target:
        return None

        return holderlist
    if len(word)!=len(target):
        return None

    if steps!=-1:
        for items in wordList:
            if len(items)==len(word):
                i=0

                if items!=word:
                    for length in items:


                        if i==1:


                            if items[1]==target[1] and items[0]==word[0] and items[2:]==word[2:]:
                                if items==target:
                                    print "Target Achieved"


                                holderlist.append(items)
                                flatten_result(holderlist,target)



                                holderlist.append(changeling(items,target,steps-1))


                        elif i>0 and i<len(word)-1 and i!=1:
                            if items[i]==target[i] and items[0:i]==word[0:i] and items[i+1:]==word[i+1:]:
                                if items==target:
                                    print "Target Achieved"
                                holderlist.append(items)
                                flatten_result(holderlist,target)

                                holderlist.append(changeling(items,target,steps-1))



                        elif i==0:
                            if items[0]==target[0] and items[1:]==word[1:]:
                                if items==target:
                                    print "Target Achieved"
                                holderlist.append(items)
                                flatten_result(holderlist,target)

                                holderlist.append(changeling(items,target,steps-1))



                        elif i==len(word)-1:
                            if items[len(word)-1]==target[len(word)-1] and items[0:len(word)-1]==word[0:len(word)-1]:
                                if items==target:
                                    print "Target Achieved"

                                holderlist.append(items)

                                holderlist.append(changeling(items,target,steps-1))

                        else:
                            return None

                        i+=1

    return holderlist
flatte_result函数允许我将列表中的列表表示为单个列表,还可以回溯我的程序。我如何在换生灵中实现扁平化结果?到目前为止,我只能在python shell中完成它。

基本上

def word_chain(chain_so_far, target, dictionary):
    last_word = chain_so_far[-1]
    if last_word == target:
        print chain_so_far
        return True
    for word in dictionary:
        if have_one_different_letter(word, last_word) and word not in chain_so_far:
            word_chain(chain_so_far + [word], target)

像这样称呼它
单词链(['love','hate',你的字典)
。如果您需要有关
有一个不同的字母()

的帮助,请告知我们,如果步骤==0和word,请正确对齐/更正代码=目标:
returnnone
returnholderlist
on块中有两条return语句。从头重写。这是一个不可读的混乱,这样一个简单的任务不需要这么多嵌套。
def word_chain(chain_so_far, target, dictionary):
    last_word = chain_so_far[-1]
    if last_word == target:
        print chain_so_far
        return True
    for word in dictionary:
        if have_one_different_letter(word, last_word) and word not in chain_so_far:
            word_chain(chain_so_far + [word], target)