2
這是一個學校項目。這是一個程序,它使用BFS返回從開始字到另一個結束字的最短路徑。我必須用起始單詞列表來檢查起始單詞並將其保存在名爲children的列表中。我想在我運行程序時打印「兒童」列表。爲什麼我在運行該程序時看不到列表中的孩子?在python中返回列表
import bintree
import imp
imp.reload(bintree)
from queuelist import Queue
class Word:
def __init__(self, w, f = None):
self.word = w
self.parent = f
filename = 'word3u'
fin=open(filename,'r')
tree = bintree.Bintree()
alist = fin.readlines()
lista='abcdefghijklmnopqrstuvwxyz'
doubles=bintree.Bintree()
for ord in alist:
word=ord.strip()
tree.put(word)
def generator(parent):
children=[]
theWord=parent.word
doubles.put(theWord)
#print(theWord)
n=0
while n<3:
for i in lista:
if n==0:
theWord=i+theWord[1:]
#print("1 "+theWord)
if n==1:
theWord=theWord[0]+i+theWord[2]
#print("2 "+theWord)
if n==2:
theWord=theWord[0:2]+i
#print("3 "+theWord)
if tree.exists(theWord):
#print("THIS " + theWord)
if not doubles.exists(theWord):
#print("THIS 2 " + theWord)
children.append(Word(theWord, parent))
doubles.put(theWord)
theWord=parent.word #reset theWord for next n
n+=1
return children
generator(Word("fan"))
它不工作。我得到一個奇怪的打印輸出 – DoubleOseven
@ Mathguy007什麼樣的輸出? – Kasramvd