提问人:Abdelrhaman Makled 提问时间:12/28/2022 最后编辑:Emi OBAbdelrhaman Makled 更新时间:12/28/2022 访问量:167
什么是“列出超出范围的索引”
what is 'list index out of range'
问:
对于此代码:
file = r<path>
import fitz
sentence = ""
count_block = 0
count_line = 0
count_element = 0
count_element_1 = 0
word_info = []
data = []
p_num = []
# lists of word information
wList = []
bList = []
lList = []
wnList = []
sList = []
# Word Coordinates
word = 4
block = 5
line = 6
word_number = 7
#
doc = fitz.open(file)
pages = len(doc)
count_1 = 0
for page in range(pages):
print(f'page:{page}')
for element in list(doc[page].get_text("words")):
print(element)
if element[4] == 'Features' or element[4].lower == 'product':
for element_1 in list(doc[page].get_text("words")):
if count_element_1 - 1 >= count_element:
wList.append(element_1[word])
bList.append(element_1[block])
lList.append(element_1[line])
count_element_1 += 1
count_element += 1
count_element_1 = 0
count_element = 0
for a, b, c in zip(wList, bList, lList):
word_info.append([a, b, c])
print(word_info)
当我尝试时,我得到:print(word_info)
[['Model', 4, 0], ['No.', 4, 0], ['KX', 4, 1], ['1504.000', 4, 1], ['Product', 5, 0], ['description', 5, 0], ['The', 5, 1], ['small', 5, 1]]
但是当我尝试时,我得到:print(word_info[0][0])
error list index out of range
注意: 这是元素的样子>>(88.046875, 562.0, 222.33563232421875, 599.5, '1504.000', 0, 0, 1)
答:
2赞
Amir Hossein Zeydi
12/28/2022
#1
The IndexError: list index out of range error
在 Python 中,当尝试访问列表中的某个项目超出该列表的索引范围时,会发生该事件。您也可以在此链接上找到答案
评论
0赞
Abdelrhaman Makled
12/28/2022
我的数组的尺寸是 400x4,所以如果我尝试打印元素 [0][0],它应该不会出错
0赞
Codist
12/28/2022
#2
长度为 N 的 Python 列表,其中 N > 0 可以由 -N 到 N-1 范围内的索引下标
任何尝试在这些限制之外对列表下标的行为都会导致 IndexError。
评论