IndexError: list index out of range錯誤總結

在做 scrapy 爬蟲的時候遇到一個錯誤“IndexError: list index out of range”

百度了一下之後,又測試了一下,發現這種錯誤一般有兩種情況:

第一種可能情況:

list[index]  index超出範圍

In [4]: li = [1,2,3]
In [5]: li[3]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-5-26fad520b253> in <module>()
----> 1 li[3]
IndexError: list index out of range
第二種可能情況:
list是一個空的,沒有一個元素

進行list[0]就會報該錯誤。

In [6]: li = []
In [7]: li[0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-7-d8d39608afdb> in <module>()
----> 1 li[0]

IndexError: list index out of range
發佈了49 篇原創文章 · 獲贊 14 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章