简体   繁体   English

While循环-IndexError:列表分配索引超出范围

[英]While-loop - IndexError: list assignment index out of range

I keep getting Index Errors when I try to use while loops in combination with lists. 当我尝试结合列表使用while循环时,我不断收到索引错误。

I wrote the code some time ago, came back to it, but just couldn't wrap my head around it enough to find my mistake. 我前一段时间写了代码,又回到了上面,但是只是无法将其包裹在头以找到我的错误。 Apparently, the error has something to do with my lists indexes being too small or too large. 显然,该错误与我的列表索引太小或太大有关。

indexes = []
#Or indexes[0], but this threw another error
indexes.append(decoded.find(special_string))

x=1
while indexes[x-1] > 0:
    total = sum(indexes)
    indexes[x] = decoded.find(special_string, total)
    x+=1

print(indexes)

My goal is to find all substrings (special_string) in a string and get their indexes (please inform me if you know an easier way to do this). 我的目标是找到字符串中的所有子字符串(special_string)并获取它们的索引(如果您知道更简单的方法,请通知我)。 I want to write all indexes out into a list for further use. 我想将所有索引写到列表中以备将来使用。

I think the only change you need to make is from: 我认为您需要进行的唯一更改是来自:

indexes[x] = decoded.find(special_string, total) to: indexes[x] = decoded.find(special_string, total)为:

indexes.append(decoded.find(special_string, total))

You can't assign indexes[x] since it doesn't exist. 您不能分配索引[x],因为它不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IndexError:使用while循环列出索引超出范围 - IndexError: list index out of range with a while loop while循环-IndexError:列表索引超出范围 - while loop - IndexError: list index out of range 当使用while循环创建“list”时,我得到此错误:IndexError:列表赋值索引超出范围 - I get this error when using a while loop to make a “list” IndexError: list assignment index out of range while-loop:列出索引超出范围过滤数据框 - while-loop: list index out of range filtering dataframe 生成 NPC 时出现“IndexError:列表分配索引超出范围” - 'IndexError: list assignment index out of range' while generating NPCs IndexError:执行TFIDF时,Python中的列表分配索引超出范围 - IndexError: list assignment index out of range in Python while performing TFIDF IndexError:字符串索引超出范围。 While-Loop 循环太多 - IndexError: string index out of range. While-Loop goes one round too much IndexError:列表分配索引超出范围 Python 3 - IndexError: list assignment index out of range Python 3 IndexError: list assignment index out of range的解决方法 - Solution for IndexError: list assignment index out of range IndexError:列表分配索引超出范围(Matplotlib) - IndexError: list assignment index out of range (Matplotlib)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM