关于Python 中IndexError:list assignment index out of range 错误解决
作者:迹忆客 时间:2021-01-27 19:06:01
在 Python 中,当您尝试访问甚至不存在的列表的索引时,会引发 IndexError: list assignment index out of range。 索引是可迭代对象(如字符串、列表或数组)中值的位置。
在本文中,我们将学习如何修复 Python 中的 Index Error list assignment index out-of-range 错误。
Python IndexError:列表分配索引超出范围
让我们看一个错误的例子来理解和解决它。
代码示例:
# error program --> IndexError: list assignment index out of range
i = [7,9,8,3,7,0] # index range (0-5)
j = [1,2,3] # index range (0-3)
print(i,"\n",j)
print(f"\nLength of i = {len(i)}\nLength of j = {len(j)}" )
print(f"\nValue at index {1} of list i and j are {i[1]} and {j[1]}")
print(f"\nValue at index {3} of list i and j are {i[3]} and {j[3]}") # error because index 3 isn't available in list j
输出:
上面代码中 IndexError: list assignment index out of range 背后的原因是我们试图访问索引 3 处的值,这在列表 j 中不可用。
修复 Python 中的 IndexError: list assignment index out of range
要修复此错误,我们需要调整此案例列表中可迭代对象的索引。 假设我们有两个列表,你想用列表 b 替换列表 a。
代码示例:
a = [1,2,3,4,5,6]
b = []
k = 0
for l in a:
b[k] = l # indexError --> because the length of b is 0
k += 1
print(f"{a}\n{a}")
输出:
IndexError: list assignment index out of range
您不能为列表 b 赋值,因为它的长度为 0,并且您试图在第 k 个索引 b[k] = I 处添加值,因此它会引发索引错误。 您可以使用 append()
和 insert()
修复它。
修复 IndexError: list assignment index out of range 使用 append() 函数
append() 函数在列表末尾添加项目(值、字符串、对象等)。 这很有帮助,因为您不必处理索引问题。
代码示例:
a = [1,2,3,4,5,6]
b = []
k = 0
for l in a:
# use append to add values at the end of the list
j.append(l)
k += 1
print(f"List a: {a}\nList b: {a}")
输出:
List a: [1, 2, 3, 4, 5, 6]
List b: [1, 2, 3, 4, 5, 6]
修复 IndexError: list assignment index out of range 使用 insert() 函数
insert()
函数可以直接将值插入到列表中的第 k 个位置。 它有两个参数,insert(index, value)。
代码示例:
a = [1, 2, 3, 5, 8, 13]
b = []
k = 0
for l in a:
# use insert to replace list a into b
j.insert(k, l)
k += 1
print(f"List a: {a}\nList b: {a}")
输出:
List a: [1, 2, 3, 4, 5, 6]
List b: [1, 2, 3, 4, 5, 6]
除了上述两种解决方案之外,如果你想像对待其他语言中的普通数组一样对待 Python 列表,你可以使用 None 值预定义你的列表大小。
代码示例:
a = [1,2,3,4,5,6]
b = [None] * len(i)
print(f'Length of a: {len(a)}')
print(f'Length of b: {len(b)}')
print(f"\n{a}\n{b}")
输出:
Length of a: 6
Length of b: 6[1, 2, 3, 4, 5, 6]
[None, None, None, None, None, None]
一旦你用虚拟值 None 定义了你的列表,你就可以相应地使用它。
来源:https://blog.csdn.net/fengqianlang/article/details/130684210
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
慎用 script 节点的 src 属性来传递参数
Python面试之os.system()和os.popen()的区别详析
Python标准库之循环器(itertools)介绍
Python实现针对给定字符串寻找最长非重复子串的方法
![](https://img.aspxhome.com/file/2023/0/131020_0s.png)
django框架中间件原理与用法详解
![](https://img.aspxhome.com/file/2023/8/83938_0s.png)
Python基于smtplib实现异步发送邮件服务
Django中处理出错页面的方法
MySQL 语言参考
php实现图片转换成ASCII码的方法
python爬虫之基金信息存储
![](https://img.aspxhome.com/file/2023/8/134518_0s.webp)
Python中if elif else及缩进的使用简述
python return逻辑判断表达式实现解析
CSS的渲染效率:书写高效的CSS
python利用后缀表达式实现计算器功能
![](https://img.aspxhome.com/file/2023/1/112011_0s.jpg)
为google量身定做的sitemap生成代码asp版
解决python路径错误,运行.py文件,找不到路径的问题
![](https://img.aspxhome.com/file/2023/1/81631_0s.jpg)
Python实现单例模式的五种写法总结
Python + selenium 自动化测试框架详解
![](https://img.aspxhome.com/file/2023/7/112947_0s.png)
Python3 伪装浏览器的方法示例
![](https://img.aspxhome.com/file/2023/1/64341_0s.jpg)