我有一个155大小的数组,我的程序包括你输入一个单词,然后在数组中搜索这个单词。
但是当我输入'176'
这是数组中的最后一个单词时,它会给出一个list index out of range
错误
这是为什么?
i = resList.index(resiID) # --searchs list and give number where found, for last word gives 155
print len(resultss) # --prints 155
colour = resultss[i] # --error given on this line
你的索引超出了界限。以下是列表索引的工作方式:
如果索引的长度是
i
,则可以使用范围0..i-1
内的任何索引。最后一个有效索引是len(mylist) - 1
。155超出范围,可能是因为您正在一个list/iterable(
resList
)中获取一个索引,并将其用作另一个/较小的list/iterable(resultss
)的索引。这是预期的行为。如果有
list
的len
为x
,则x
索引未定义。例如:
相关问题 更多 >
编程相关推荐