【发布时间】:2014-10-23 22:03:06
【问题描述】:
entry="Where in the world is Carmen San Diego"
goal=["Where in the", "world is", "Carmen San Diego"]
我正在尝试创建一个过程,该过程将在“条目”中搜索属于“目标”列表成员的词块。我想保留这些子集中的词序。
这是我目前所拥有的。我不确定如何完成此操作,或者我是否以正确的方式接近它。
span=1
words = entry.split(" ")
initial_list= [" ".join(words[i:i+span]) for i in range(0, len(words), span)]
x=len(initial_list)
initial_string= " ".join(initial_list)
def backtrack(A,k):
if A in goal:
print
else:
while A not in goal:
k=k-1
A= " ".join(initial_list[0:k])
if A in goal:
print A
words=A.split(" ")
firstmatch= [" ".join(words[i:i+span]) for i in range(0, len(words), span)]
newList = []
for item in initial_list:
if item not in firstmatch:
newList.append(item)
nextchunk=" ".join(newList)
backtrack(initial_string,x)
目前的输出是这样的:
"Where in the"
期望的输出:
"Where in the"
"world is"
"Carmen San Diego"
我一直在努力寻找合适的算法,我认为它需要回溯或搜索修剪,我不太确定。理想情况下,解决方案适用于任何“条目”和“目标”列表。非常感谢任何 cmets。
【问题讨论】:
-
您的示例对于理解您要做什么并不是特别有帮助。如果您有
entry = "abcabcdefdef"和goal = ["ab", "dd", "c"],您希望输出什么? -
@BrandonHumpert。在那种情况下,我不希望打印任何内容。总的来说,这是一个原型。这个“目标”列表实际上代表了一组成功的 JSON 查询。 “entry”将是用户输入的字符串。我想以我所描述的“回溯”方式将此用户条目分解为多个查询字符串。希望这更清楚。
标签: python search backtracking pruning