我已经打开了另一个主题正是这个主题,但是我觉得我发布了太多的代码而且我真的不知道我的问题在哪里,现在我觉得我有更好的想法,但仍然需要帮助。我们所拥有的是一个包含3个字母单词的文本文件,只有3个字母单词。我还有一个Word(节点)和队列类。我的findchildren方法应该为一个单词找到所有孩子这个词,让我说我进入“粉丝”,然后我应该得到类似[“kan”,“man”....等等]。该代码目前看起来像这样:
def findchildren(mangd,parent):
children=set()
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:
if word not in children:
children.add(word)
if i>2:
break
return children
上面的代码,对于findchildren目前工作正常,但是,当我将它用于我的其他方法(实现bfs-search)时,一切都会花费太长时间,因此,我想收集所有的孩子在包含子项列表的字典中。感觉这个任务现在不属于我的联盟,但这可能吗?我试图创造这样的东西:
def findchildren2(mangd):
children=[]
for word in mangd:
lparent=list(word)
mangd.remove(word)
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:
if word not in children:
children.append(word)
if i>2:
break
return children
我想我的最后一次尝试只是垃圾,我得到错误消息“使用迭代设置更改大小”。
def findchildren3(mangd,parent):
children=defaultdict(list)
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[0].append(word)
if i>2:
break
return children
答案 0 :(得分:0)
有更有效的方法可以做到这一点(下面是O(n ^ 2)所以不是很好)但这里有一个简单的算法可以帮助你入门:
import itertools
from collections import defaultdict
words = ['abc', 'def', 'adf', 'adc', 'acf', 'dec']
bigrams = {k: {''.join(x) for x in itertools.permutations(k, 2)} for k in words}
result = defaultdict(list)
for k, v in bigrams.iteritems():
for word in words:
if k == word:
continue
if len(bigrams[k] & bigrams[word]):
result[k].append(word)
print result
产地:
defaultdict(<type 'list'>, {'abc': ['adc', 'acf'], 'acf': ['abc', 'adf', 'adc'], 'adf': ['def', 'adc', 'acf'], 'adc': ['abc', 'adf', 'acf', 'dec'], 'dec': ['def', 'adc'], 'def': ['adf', 'dec']})
这是一个更高效的版本,带有一些评论:
import itertools
from collections import defaultdict
words = ['abc', 'def', 'adf', 'adc', 'acf', 'dec']
# Build a map of {word: {bigrams}} i.e. {'abc': {'ab', 'ba', 'bc', 'cb', 'ac', 'ca'}}
bigramMap = {k: {''.join(x) for x in itertools.permutations(k, 2)} for k in words}
# 'Invert' the map so it is {bigram: {words}} i.e. {'ab': {'abc', 'bad'}, 'bc': {...}}
wordMap = defaultdict(set)
for word, bigramSet in bigramMap.iteritems():
for bigram in bigramSet:
wordMap[bigram].add(word)
# Create a final map of {word: {words}} i.e. {'abc': {'abc', 'bad'}, 'bad': {'abc', 'bad'}}
result = defaultdict(set)
for k, v in wordMap.iteritems():
for word in v:
result[word] |= v ^ {word}
# Display all 'childen' of each word from the original list
for word in words:
print "The 'children' of word {} are {}".format(word, result[word])
产地:
The 'children' of word abc are set(['acf', 'adc'])
The 'children' of word def are set(['adf', 'dec'])
The 'children' of word adf are set(['adc', 'def', 'acf'])
The 'children' of word adc are set(['adf', 'abc', 'dec', 'acf'])
The 'children' of word acf are set(['adf', 'abc', 'adc'])
The 'children' of word dec are set(['adc', 'def'])
答案 1 :(得分:0)
解决方案(遗憾的是O(n ^ 2))用于Python 3中的更新要求(运行它here):
from collections import defaultdict
words = ['fan', 'ban', 'fbn', 'ana', 'and', 'ann']
def isChildOf(a, b):
return sum(map(lambda xy: xy[0] == xy[1], zip(a, b))) >= 2
result = defaultdict(set)
for word in words:
result[word] = {x for x in words if isChildOf(word, x) and x != word}
# Display all 'childen' of each word from the original list
for word in words:
print("The children of word {0} are {1}".format(word, result[word]))
产地:
The 'children' of word fan are set(['ban', 'fbn'])
The 'children' of word ban are set(['fan'])
The 'children' of word fbn are set(['fan'])
The 'children' of word ana are set(['and', 'ann'])
The 'children' of word and are set(['ann', 'ana'])
The 'children' of word ann are set(['and', 'ana'])
这里的算法很简单,但效率不高,但我试着把它分解。
isChildOf
函数将两个单词作为输入并执行以下操作:
zip
的{{1}}&amp; a
在一起,这里都被视为迭代,每个字符在迭代中都是一个“项”。例如,如果b
为a
且'fan'
为b
,则'ban'
会生成此对列表zip('fan', 'ban')
接下来,它使用[('f', 'b'), ('a', 'a'), ('n', 'n')]
函数将lambda函数(匿名函数的奇特名称)应用于步骤1中生成的列表中的每个项。该函数只需获取一对输入元素(即map
&amp; 'f'
),如果匹配则返回'b'
,否则返回True
。对于我们的示例,这将导致False
,因为第一对字符不匹配,但其余对都匹配。
最后,该函数在步骤2生成的列表上运行[False, True, True]
函数。事实上,sum
在Python中评估为True
,1
为False
0
所以我们的列表总和为2
。然后,我们只返回该数字是否大于或等于2
。
for word in words
循环只是将每个输入词与所有其他词进行比较,并将isChildOf
评估的词保持为True
,注意不要添加词本身。
我希望这很清楚!