关于python list index out of range
Posted cici0089621
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于python list index out of range相关的知识,希望对你有一定的参考价值。
我写的这个 去除 字符串中多余空格,(所有空格处 只保留一个空格)的 方法
如果发生 remove 那么 len(l)的总大小就会减少,而 i的 最大值不变,所以会暴这个错误
def removeblank(line):
str=line.strip()
l=strtolist(str)
for i in range(len(l)):
if (l[i] == ‘ ‘ and i+1<len(l)):
a=l[i]
for j in range(i+1,len(l)):
b=l[j]
if l[j]==‘ ‘:
l.remove(l[j])
break
return l
考虑一下 使用 do while的 方式 在判断条件时 应该每次都会 判断一下
以上是关于关于python list index out of range的主要内容,如果未能解决你的问题,请参考以下文章
Python常见错误:IndexError: list index out of range
python 列表 越界怎么处理?list index out of range
python-爬取中国大学排名网站信息IndexError:list index out of range
python-爬取中国大学排名网站信息IndexError:list index out of range
Python_报错:SyntaxError: 'break' outside loopIndexError: list assignment index out of range(示例