Python 递归和附加到列表
我在一个程序中遇到了问题,这个程序需要一个单词,一次改变一个字母,将这个单词转换成目标单词。尽管如此,请记住,根据给我的一本词典,转换后的单词必须是合法单词 我很难弄清楚如何使它递归。该程序对必须采取的步骤数量有限制 输出需要是一个列表。因此,如果功能转换的参数为Python 递归和附加到列表,python,list,loops,recursion,append,Python,List,Loops,Recursion,Append,我在一个程序中遇到了问题,这个程序需要一个单词,一次改变一个字母,将这个单词转换成目标单词。尽管如此,请记住,根据给我的一本词典,转换后的单词必须是合法单词 我很难弄清楚如何使它递归。该程序对必须采取的步骤数量有限制 输出需要是一个列表。因此,如果功能转换的参数为 转换(“查找”、“丢失”),输出应为: ['find'、'fine'、'line'、'lone'、'lose'] 使用我当前的代码: def changeling(word,target,steps): holderlist=[] i
转换(“查找”、“丢失”),输出应为: ['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
def更换(字、目标、步骤):
持有者列表=[]
i=0
如果步骤0和i如果您试图将列表添加到列表中,您可能希望
扩展
,而不是附加
我不完全相信使用
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']
这里有一个替代实现。它不使用递归,而是使用置换。它被重写以传递单词列表,而不是依赖于全局单词列表,这将使它更具可移植性。这个实现也严格依赖于生成器,这确保了比扩展列表更小的内存占用(如extend/append解决方案)
w[-2],w[0]
可以修改/替换以匹配您选择的任何单词我不确定如何在换生灵中实施此程序。我可以在python shell中使用它,它可以工作,但是考虑到changeling是递归的,有些地方出了问题,我得到了原始输出。另外,你知道如何让输出包含changeling的第一个单词吗,我可以将changeling改为holderlist=[word],但后来我得到了一大堆副本。作为对你现在被删除的问题的回答:试着找到一个对你有用的程序(比如检索一些天气数据或新闻,或者计算你的打字速度或其他什么),然后编写程序。开始时保持简单,然后添加一些内容。
import itertools
somelists = [['find','fine','line','lone','lose'],
['bank','hank','hark','lark','lurk'],
['tank','sank','sink','sing','ding']]
def changeling(word, target, wordlist):
def difference(word, target):
return len([i for i in xrange(len(word)) if word[i] != target[i]])
for length in xrange(1, len(wordlist) + 1):
for possibilities in [j for j in itertools.permutations(wordlist, length) if j[0] is word and j[-1] is target]:
#computes all permutations and discards those whose initial word and target word don't match parameters
if all(difference(possibilities[i], possibilities[i+1]) == 1 for i in xrange(0, len(possibilities) - 1)):
#checks that all words are exactly one character different from previous link
return possibilities
#returns first result that is valid; this can be changed to yield if you wish to have all results
for w in somelists:
print "from '%s' to '%s' using only %s" % (w[-2], w[0], w)
print changeling(w[-2], w[0], w)
print