对于我创建的一种方法,我想在单词末尾找到一个单词,然后我想要将发现的单词附加到左侧(在行的开头直到一个空格字符)到一个数组。Python - 在给定输入的同一行上查找单词
这是到目前为止我的代码:
def ruleElements(factor):
# Creates list of RHS and LHS rule elements
results = []
# If RHS factor is found in grammar, append corresponding LHS.
for line in grammarFile:
start = line.find(0)
end = line.find(' ', start)
if factor in line:
results.append(line[start:end])
return results
到目前为止输出的阵列出现空所有的时间。不知道我的逻辑错在哪里。
在grammarFile甲行看起来像,例如:
VP> - V NP
NP - >Ñ
VP - > V PP
我想利用部分在 - >的右侧作为输入,并将左侧追加到要在程序的其他部分中使用的数组。
谢谢。这是我需要的。 –