for i in range(len(text_list)):
for j in range(len(text_list[i])):
if text_list[i][j] in ban:
text_list[i]=text_list[i][:j]
第三行出现错误。 我不知道该怎么做。 我觉得不应该有任何错误..python3.x:索引超出范围
for i in range(len(text_list)):
for j in range(len(text_list[i])):
if text_list[i][j] in ban:
text_list[i]=text_list[i][:j]
第三行出现错误。 我不知道该怎么做。 我觉得不应该有任何错误..python3.x:索引超出范围
此行改变了text_list[i]
长度:
text_list[i]=text_list[i][:j]
所以for j ...
下一个迭代是无效的。也许在修改text_list
后,您应该将break
列出到更高的for i ...
循环中。
您正在迭代期间修改列表。如果你的条件成功,你会削减该行的其余部分。所以在下一次迭代中,你会遇到一个出界的错误。这与写作基本相同:
arr = [1, 2, 3]
for i in range(len(arr)):
print(arr[i])
arr = arr[:i] # could be simply arr = []
绝不会在迭代过程中改变列表,因为索引将会移位。相反,创建一个新的列表,也许看看列表的理解:http://python-3-patterns-idioms-test.readthedocs.io/en/latest/Comprehensions.html
你的变量是什么? “text_list”究竟是什么?什么是'禁令'? – TheDarkKnight
ban = ['','\ t','\ n','。',',',';',':',' - ',' - ',' - ','[',' ]','{','}','(',')',''',''', “'”,'!','?','/','@','# ','$','%','^','&',] –
请编辑您的问题以提供最小,完整和可验证的示例:https://stackoverflow.com/help/mcve –