indexerror_解决IndexError:Python中的列表索引超出范围

indexerror

Hello readers, welcome back to a yet another post of The Crazy Programmer. Today I’ll be discussing one of the common error programmers make when they start with programming and that is IndexError. Now before talking about this error, let’s try to see what an Index is and why this error occurs.

读者好,欢迎回到“疯狂程序员”的另一篇文章。 今天,我将讨论程序员在开始编程时遇到的常见错误之一,即IndexError 。 现在,在讨论此错误之前,让我们尝试看看什么是索引以及为什么会发生此错误。

An index is a location of an item in an Array or a List and in most of the programming language, it starts from 0. So when we count the elements in a list, we’ll count them from 1 but when we’ll try to access the elements of the list, we’ll start our index from 0. Let us understand this with an example. Please don’t skip the comments.

索引是项目在数组或列表中的位置,在大多数编程语言中,它从0开始。 因此,当我们计算列表中的元素时,我们将从1开始对其进行计数,但是当我们尝试要访问列表的元素,我们将从0开始索引。让我们通过一个示例来理解这一点。 请不要跳过评论。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
color = ['red', 'green', 'blue', 'black', 'white']
# index    0       1        2       3        4
# here we can see that we have 5 colors in our color list
# but when we'll try to print these items from our list
# we can't get color[1] as red
# this is because we may find red to be the first element
# but technically it is not at index 1
# to print red, we need to use 0 as the index and similarly
# for other colors, we have to use the index accordingly
print(color[0]) # 0 for red
print(color[1]) # 1 for green
print(color[2]) # 2 for blue
print(color[3]) # 3 for black
print(color[4]) # 4 for white
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
color = [ 'red' , 'green' , 'blue' , 'black' , 'white' ]
# index    0       1        2       3        4
# here we can see that we have 5 colors in our color list
# but when we'll try to print these items from our list
# we can't get color[1] as red
# this is because we may find red to be the first element
# but technically it is not at index 1
# to print red, we need to use 0 as the index and similarly
# for other colors, we have to use the index accordingly
print ( color [ 0 ] ) # 0 for red
print ( color [ 1 ] ) # 1 for green
print ( color [ 2 ] ) # 2 for blue
print ( color [ 3 ] ) # 3 for black
print ( color [ 4 ] ) # 4 for white

So we can say that green is the 2nd element of the color list but we cannot say that it is at index ‘2’. Similarly white is at 5th position but it is not at 5th index. If we’ll try to access white and if we’ll use print(color[5]) to access white, then it is going to give us an error i.e. IndexError: list index out of range.

因此我们可以说绿色颜色列表的第二个元素,但是我们不能说它在索引“ 2”处。 同样, 白色 5位,但不在 5 。 如果我们尝试访问白色并且如果使用print(color [5])访问白色 ,那么它将给我们一个错误,即IndexError:列表索引超出范围

index error

Here we all can see that I’m trying to print the value of a list at an index ‘5’ but here in the array, we don’t have any element at that index.

在这里,我们所有人都可以看到我正在尝试在索引“ 5”处打印列表的值,但是在数组中,该索引处没有任何元素。

Now the interpreter will try to parse this code and it will not find any value at 4th index and ultimately it will fail to print the value.

现在,解释器将尝试解析此代码,并且它将在第4个索引处找不到任何值,最终它将无法打印该值。

So now I think it is clear why IndexError occurs and how you can avoid them. Still, if you stuck into any kind of error, don’t forget to google it and try to debug it on your own. This will teach you how to debug your code as someone might have faced a similar problem earlier.

所以现在我认为很清楚为什么会发生IndexError以及如何避免它们。 不过,如果您遇到任何类型的错误,请不要忘记使用Google搜索并尝试自行调试。 这将教您如何调试代码,因为以前有人可能遇到过类似的问题。

If you still don’t find any solution for your problem, you can ask your doubt in the comment’s section below and we’ll get back to you🤓.

如果您仍然找不到解决问题的方法,可以在下面的评论部分中提出疑问,我们会尽快与您联系🤓。

This was all for today. Thanks for your visit and if you are new here, consider subscribing to our newsletter. See you in my next post. Bye!

这就是今天的全部。 感谢您的访问,如果您是这里的新手,请考虑订阅我们的时事通讯。 在下一篇文章中见。 再见!

翻译自: https://www.thecrazyprogrammer.com/2020/05/solve-indexerror-list-index-out-of-range-in-python.html

indexerror

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值