我需要迭代我的单词列表,并在循环中增量两次。通过第一次增量,我将单词保存在 l 中,通过第二次增量,我将标签保存在 m 中,这些标 checkout 现在 i+1 处。我需要使用单个变量(即 i 这里)迭代循环。但它给了我一个错误:列表索引超出范围。
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
print(l)
i +=1
m=words[i]
print(m)
if result!=0:
#do something
else:
#do something
i+=1
最佳答案
这尚未经过测试,但这里是:
while i<len(words)-1: # this loop store words of a file in an array
l=lemmatizer.lemmatize(words[i]) #array of words is lematized here
print(l)
m=words[i+1]
print(m)
if result!=0:
#do something
else:
#do something
i+=2
-- 我认为 for 循环在这里会更好,但是 while 也有效。
关于python - 如何增加两次而不出现列表索引超出范围的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895561/