返回python中的列表

2024-05-21 05:55:49 发布

您现在位置:Python中文网/ 问答频道 /正文

这是学校的一个项目。它是一个程序,返回最短路径从一个开始字到另一个结束字使用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"))

Tags: importself程序tree列表if单词word