python - 如何增加两次而不出现列表索引超出范围的错误

标签 python arrays string list

我需要迭代我的单词列表,并在循环中增量两次。通过第一次增量,我将单词保存在 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/

相关文章:

java - 在哪里寻找以获取 Java 类库中使用的 String 常量的可能值

python - 多个 For 循环 Django

python - 类型错误 : inputNames() takes no arguments (2 given)

javascript - 如何将值存储在数组中

c - 访问C中的外部分配空间

c - 如何在C中将一个字符串拆分为2个字符串

python - 关于用 python 抓取 html 的说明

python - 在Python中简单添加

java - 如何在xsl中显示Java的String类型数组?

c++ - 如何将 ostream_iterator 的输出转换为字符串?