所以我的代码,目前正在寻找这样的:BFS问题,试图找到“孩子”,我在我的方法似乎柜台是错误的
def hamta():
ordlista=[]
fil=open("labb9text.txt")
ordlista=[]
for line in fil.readlines():
ordlista.append(line.strip())
return ordlista
def setlista():
ordlista=hamta()
setlista=set()
for a in ordlista:
if a not in setlista:
setlista.add(a)
return setlista
def hittabarn(parent):
mangd=setlista()
children=[]
lparent=list(parent)
mangd.remove(parent)
for word in mangd:
letters=list(word)
count=0
i=0
for a in letters:
if a==lparent[i]:
count+=1
i+=1
else:
i+=1
if count>=2:
children.append(word)
if i==2:
break
return children
文件labb9text.txt充满包含三个字母的单词,如粉丝,男人,ulk(简单的瑞典语)。在hamta我只想得到像这样的单词列表,在setlista我不想要任何重复,我希望他们以随机顺序插入。当谈到Hittabarn时,我希望它能够搜索我的“setlista”并找到“孩子们”。
一个例子是,如果我在hittabarn方法中使用单词“fan”作为参数,那么我希望它找到像man,kan,lan这样的单词(我只在这里切换了第一个字母,但我可以以及如果在我的列表中找到fsn)。孩子与其他单词是相同的单词,但是一个字母被交换,并不重要。我的hamta()和setlista()正在尽我所能地工作,但是我不能让我的工作变得简单,有什么想法会出错?
你可以养活一个迭代符设置像这样:'设置([1,1,2,3,4,4])'摆脱重复 – enthus1ast
使用'对于我,在枚举(字母):'不需要计数器变量 – enthus1ast
我试图删除计数器,只是使用该for-loop,但我得到一个错误消息(需要超过1个值解开) –