python - 递归和 append 到列表

标签 python list loops recursion append

我在使用一个程序时遇到问题,该程序接受一个单词,一次更改一个字母,将该单词转换为目标单词。不过,请记住,根据我得到的单词词典,转换后的单词必须是合法单词。

我无法弄清楚如何使其递归。该程序对其必须执行的步数有限制。

输出需要是一个列表。所以如果函数 changeling 的参数是
changeling("find","lose"),输出应该是: ['find','fine','line','lone','lose'].

使用我当前的代码:

def changeling(word,target,steps):
holderlist=[]
i=0
if steps<0 and word!=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"
                                holder.list.append(target)
                            holderlist.append(items)
                            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)
                            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)
                            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

我收到一个困惑的输出: ['很好', ['线', ['孤独', ['失去', []]]], '喜欢', []]

我得到了我想要的答案,但我不确定如何 a) 通过不在列表中包含列表来清理它。并且 b)fond 出现了,因为当调用 find 时它给出了 fine 和 fond,fine 是以目标词结尾的那个,fond 失败了,但是我不确定一旦我 append 了它如何摆脱它它到 holderlist。

如有任何帮助,我们将不胜感激。

干杯。

最佳答案

我不完全相信使用 extend 而不是 append 会解决你所有的问题,因为看起来这可能无法解释不做的改变导致解决这个词,需要回溯。

如果事实证明我是正确的并且其他答案最终没有奏效,这里是一个递归函数,可以将您当前的结果转换为您正在寻找的结果:

def flatten_result(nested_list, target):
    if not nested_list:
        return None
    for word, children in zip(nested_list[::2], nested_list[1::2]):
        if word == target:
            return [word]
        children_result = flatten_result(children, target)
        if children_result:
            return [word] + children_result
    return None

>>> result = ['fine', ['line', ['lone', ['lose', []]]], 'fond', []]
>>> flatten_result(result, 'lose')
['fine', 'line', 'lone', 'lose']

关于python - 递归和 append 到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9876927/

相关文章:

iphone - 滚动滚动的 iOS 列表元素的名称

PHP 获取给定目录的所有子目录

java - 保持循环直到输入在 java 中可用

java - 将数字保存到循环JAVA中的变量

algorithm - 如何回答这个递归问题,它和循环之间有很大的区别吗

python - 一维数组或列表的隔离森林 Sklearn 以及如何调整超参数

python - 请问这个功能如何修复?

java - 创建非常大的列表 block 时内存不足

python - 计算两个系列之间的工作日

python - 修改绘图轴,使其刻度标签的顺序和它们各自的点相应地改变——而不修改数据本身