这是学校的一个项目。它是一个程序,返回最短路径从一个开始字到另一个结束字使用BFS。我必须用我的单词列表交叉检查开头的单词,并将该单词保存在一个名为children的列表中。我想在运行程序时打印孩子的列表。为什么我在运行程序时看不到孩子们的列表?你知道吗
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"))
因为在函数中返回了
childeren
,所以在调用时需要打印对象是的。所以呢只需执行以下操作:相关问题 更多 >
编程相关推荐