是否出现“IndexError:列表索引超出范围”? 当尝试访问第 N 个项目时意味着我的列表少于 N 个项目?

发布于 2024-07-26 16:02:32 字数 53 浏览 9 评论 0原文

我告诉我的程序打印输出的第 53 行。 这个错误是否告诉我没有那么多行,因此无法打印出来?

I'm telling my program to print out line 53 of an output. Is this error telling me that there aren't that many lines and therefore can not print it out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

林空鹿饮溪 2024-08-02 16:02:32

如果您有一个包含 53 项的列表,则最后一项是 thelist[52],因为索引从 0 开始。


来自 真正的 Python:了解 Python 回溯 - IndexError

索引错误

当您尝试从序列(例如列表元组)中检索索引时,会引发IndexError,并且该索引不存在在序列中找不到。 Python 文档定义了何时引发此异常:

<块引用>

当序列下标超出范围时引发。 (来源

下面是引发 IndexError 的示例:

test = list(range(53))
test[53]

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-6-7879607f7f36> in <module>
      1 test = list(range(53))
----> 2 test[53]

IndexError: list index out of range

IndexError 的错误消息行不会为您提供重要信息。 您可以看到您有一个超出范围的序列引用以及序列的类型,在本例中为列表。 这些信息与回溯的其余部分相结合,通常足以帮助您快速确定如何解决问题。

If you have a list with 53 items, the last one is thelist[52] because indexing starts at 0.


From Real Python: Understanding the Python Traceback - IndexError:

IndexError

The IndexError is raised when you attempt to retrieve an index from a sequence, like a list or a tuple, and the index isn’t found in the sequence. The Python documentation defines when this exception is raised:

Raised when a sequence subscript is out of range. (Source)

Here’s an example that raises the IndexError:

test = list(range(53))
test[53]

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-6-7879607f7f36> in <module>
      1 test = list(range(53))
----> 2 test[53]

IndexError: list index out of range

The error message line for an IndexError doesn’t give you great information. You can see that you have a sequence reference that is out of range and what the type of the sequence is, a list in this case. That information, combined with the rest of the traceback, is usually enough to help you quickly identify how to fix the issue.

挽你眉间 2024-08-02 16:02:32

是的,

您正在尝试访问列表中不存在的元素。

MyList = ["item1", "item2"]
print MyList[0] # Will work
print MyList[1] # Will Work
print MyList[2] # Will crash.

你有过差一错误吗?

Yes,

You are trying to access an element of the list that does not exist.

MyList = ["item1", "item2"]
print MyList[0] # Will work
print MyList[1] # Will Work
print MyList[2] # Will crash.

Have you got an off-by-one error?

女皇必胜 2024-08-02 16:02:32

Python 索引的工作方式是从 0 开始,因此列表的第一个数字将为 [0]。 你必须打印[52],因为起始索引是0并且
因此第 53 行是 [52]

从该值中减去 1 就应该没问题了。 :)

The way Python indexing works is that it starts at 0, so the first number of your list would be [0]. You would have to print[52], as the starting index is 0 and
therefore line 53 is [52].

Subtract 1 from the value and you should be fine. :)

花之痕靓丽 2024-08-02 16:02:32

是的。 该序列没有第 54 项。

Yes. The sequence doesn't have the 54th item.

山人契 2024-08-02 16:02:32

这是正确的。 “列表索引超出范围”很可能意味着您正在引用列表的第 n 个元素,而列表的长度小于 n。

That's right. 'list index out of range' most likely means you are referring to n-th element of the list, while the length of the list is smaller than n.

奈何桥上唱咆哮 2024-08-02 16:02:32

当您想要克服此错误时,请始终记住,索引和范围的默认值从 0 开始,因此如果总项目数为 100,则 l[99] 和 range(99) 将允许您访问最后一个元素。

每当您遇到此类错误时,请交叉检查范围之间/中间的项目,并确保它们的索引不是最后一个,如果您获得输出,那么您已经犯了上述完美错误。

Always keep in mind when you want to overcome this error, the default value of indexing and range starts from 0, so if total items is 100 then l[99] and range(99) will give you access up to the last element.

whenever you get this type of error please cross check with items that comes between/middle in range, and insure that their index is not last if you get output then you have made perfect error that mentioned above.

不气馁 2024-08-02 16:02:32

如果您从文本文件中读取列表,您可能会得到最后一个空行作为列表元素。
你可以像这样摆脱它:

list.pop()
for i in list:
   i[12]=....

If you read a list from text file, you may get the last empty line as a list element.
You can get rid of it like this:

list.pop()
for i in list:
   i[12]=....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文