python - 在 python 中使用 pop 时获取索引超出范围错误

标签 python list indexoutofrangeexception

我正在尝试读取一个文本文件并从中删除所有停用词。但是,我在使用 b[i].pop(j) 时遇到索引超出范围错误。 但是,如果我使用 print(b[i][j]),我不会收到任何错误并将单词作为输出。 谁能发现错误?

import nltk
from nltk.corpus import stopwords
stop = stopwords.words('english')

fo = open("text.txt", "r")
# text.txt is just a text document

list = fo.read();
list = list.replace("\n","")
# removing newline character

b = list.split('.',list.count('.'))
# splitting list into lines

for i in range (len(b) - 1) :
    b[i] = b[i].split()
# splitting each line into words

for i in range (0,len(b))   :
    for j in range (0,len(b[i]))    :
        if b[i][j] in stop  :
            b[i].pop(j)
#           print(b[i][j])
#print(b)

# Close opend file
fo.close()

输出:

Traceback (most recent call last):
  File "prog.py", line 29, in <module>
    if b[i][j] in stop  :
IndexError: list index out of range

注释 b[i].pop(j) 和取消注释 print(b[i][j]) 的输出:

is
that
the
from
the
the
the
can
the
and
and
the
is
and
can
be
into
is
a
or

最佳答案

当你迭代它们时,你正在从列表中删除元素,这会导致列表在迭代期间缩小大小,但迭代仍会继续原始列表的长度,因此导致这样的 InderError 问题。

您应该尝试创建一个仅包含所需元素的新列表。示例 -

result = []
for i in range (0,len(b)):
    templist = []
    for j in range (0,len(b[i])):
        if b[i][j] not in stop :
            templist.append(b[i][j])
    result.append(templist)

同样可以在列表理解中完成-

result = [[word for word in sentence if word not in stop] for sentence in b]

关于python - 在 python 中使用 pop 时获取索引超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33211824/

相关文章:

python - 我可以从一个类获取 rect.x 以在另一类中使用吗?

python - 如何在 Python 中使用循环反转列表的一部分?

c - 在C中创建链表时程序崩溃

c# - 在 C# 中使用列表

python - 实现一种让我的 tron 角色无法通过自身的方法

python - 如何从python 3中的url读取html

python - python 中是否有广泛使用的 MySQL 包装器/API?

c# - WinForms DataGridView 抛出 IndexOutOfRangeException

c# - 如何处理 System.IndexOutOfRangeException

ios - 尝试直接转到数组末尾时发生索引超出范围错误