【发布时间】:2018-02-03 11:44:23
【问题描述】:
我想匹配列表中的元素并将匹配后的下一个索引附加到数组列表中,但在我的代码中它给了我一个空数组。我不知道问题出在哪里。在我的代码中,结果也是一个数组,其中包含一个名为 'hilalitag.txt' 的文件,该文件拆分为多个单词。但为了简单起见,我在这里提到了一个包含少量值的数组。我想将结果元素与单词元素进行匹配,如果在单词中找到匹配项,那么我需要在名为 arr[] 的数组中追加匹配后的下一个单词索引。
sample_text='ahmedrazatg.txt'
lemmatizer=WordNetLemmatizer()
file = open(sample_text,'r')
arr=[]
result=[]
for line in file.readlines():
words=re.split(' |/|:|;|,|-RRB-|-LRB-|!|\*|\*\*|``',line) #removing punctuations from file
words=[line.replace(".","") for line in words] #removing full stop from file
i=4
while i<len(words): # this loop store words of a file in an array
l=lemmatizer.lemmatize(words[i]) #array of words is lematized here
i += 1
result=['Alphabets','reveals','help','opinions','Allah']
j=4
for j in words:
if j in result:
arr.append(j+1)
print(arr)
【问题讨论】:
-
1.减少解释 2. 更少代码,更多示例 3. 一些输入和预期输出会有所帮助