iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python 中IndexError: list assignment index out of range 错误解决
  • 640
分享到

Python 中IndexError: list assignment index out of range 错误解决

python开发语言 2023-10-12 11:10:11 640人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

文章目录 Python IndexError:列表分配索引超出范围修复 Python 中的 IndexError: list assignment index out of range修复 I

文章目录


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 rangei = [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

输出:

Python 中IndexError- list assignment index out of range

上面代码中 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 = 0for l in a:    b[k] = l # indexError --> because the length of b is 0    k += 1print(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 = 0for l in a:   # use append to add values at the end of the list    j.append(l)    k += 1print(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 = 0for l in a:    # use insert to replace list a into b    j.insert(k, l)    k += 1print(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: 6Length of b: 6[1, 2, 3, 4, 5, 6][None, None, None, None, None, None]

一旦你用虚拟值 None 定义了你的列表,你就可以相应地使用它。


总结

可能有更多的手动技术和逻辑来处理 IndexError:Python 中的列表分配索引超出范围。 本文概述了两个常见的列表函数,它们可以帮助我们在替换两个列表时帮助我们处理 Python 中的索引错误。

我们还讨论了预定义列表并将其视为类似于其他编程语言数组的数组的替代解决方案。

来源地址:https://blog.csdn.net/fengqianlang/article/details/130684210

--结束END--

本文标题: Python 中IndexError: list assignment index out of range 错误解决

本文链接: https://www.lsjlt.com/news/429304.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作