转 Python-IndexError: list index out of range
https://www.cnblogs.com/2bjiujiu/p/9063864.html
Error:IndexError: list index out of range
Where?
对Python中有序序列进行按索引取值的时候,出现这个异常
Why?
对于有序序列: 字符串 str 、列表 list 、元组 tuple进行按索引取值的时候,默认范围为 0 ~ len(有序序列)-1,计数从0开始,而不是从1开始,最后一位索引则为总长度减去1。当然也可以使用 负数表示从倒数第几个,计数从-1开始,则对于有序序列,总体范围为 -len(有序序列) ~ len(有序序列)-1,如果输入的取值结果不在这个范围内,则报这个错
Way?
检查索引是否在 -len(有序序列) ~ len(有序序列)-1 范围内,修改正确
错误代码:
|
1
2
3
4
|
name = "beimenchuixue"students = ["beimenchuixue", "boKeYuan", "Python", "Golang"]print(name[20])print(students[4]) |
正确代码:
|
1
2
3
4
|
name = "beimenchuixue"students = ["beimenchuixue", "boKeYuan", "Python", "Golang"]print(name[3])print(students[3]) |
##########for 循环 遇到 IndexError:
https://www.jianshu.com/p/f1b58ec12b72
失败原因:爬虫在做xpath时候匹配到空值,粗心大意以为网页很简单,没仔细看,因为标识tr还有空值
解决方法:加上try.....except 错误机制跳过tr空值
try:
#寻找tr下面的标识
qihao = item.select('td strong')[0].text
onewei = item.select('td div')[0].text
twowei = item.select('td div')[1].text
threewei = item.select('td div')[2].text
fourwei = item.select('td div')[3].text
fivewei = item.select('td div')[4].text
sixwei = item.select('td div')[5].text
sevenwei = item.select('td div')[6].text
list = [qihao,onewei,twowei,threewei,fourwei,fivewei,sixwei,sevenwei]
all_lists.append(list)
print(list)
except IndexError:
pass
转 Python-IndexError: list index out of range的更多相关文章
- Python常见错误:IndexError: list index out of range
用python写脚本查询字典时,在遍历字典时循环到某一项时老是报错 出现这种错误有两种情况: 第1种可能情况 list[index]index超出范围 第2种可能情况 list是空值就会出现 In ...
- python报错IndexError: list index out of range
今天写个ping vpn的python脚本,报错IndexError: list index out of range 最后查看是python读取文件中出现空格 去掉空格即可
- pyinstaller 将.py生成.exe ----报错 “IndexError: tuple index out of range”
pyinstaller将py打包为exe文件,用pysintaller居然报错 File "c:\anaconda3\lib\site-packages\PyInstaller\depend ...
- IndexError:string index out of range
IndexError:string index out of range 出现在下标越界的情况,如 item[1],可能为空的时候下标就会越界
- 在数组添加元素时报错:IndexError: list index out of range
今天第一次发随笔还有许多不足之处,欢迎评论!!! 最近在写一个成语接龙的小游戏,结果在数组添加元素时报错:IndexError: list index out of range 源码: import ...
- mac安装MySQLdb:IndexError: string index out of range
使用mac安装MySQLdb的时候出现string index out of range 大概的错误是这样的: 然后尝试手动安装,我下载了包后,依然出现这个错误. 于是百度了下: https://ww ...
- IndexError: list index out of range Python常见错误
引用超过list最大索引,此错误非常常见,注意列表的元素个数 ----------------------------------------------
- MacOS 安装MysqlDB 问题解决方案( 解决 IndexError: string index out of range)
pip install MySQL-python时报错如下: Command "python setup.py egg_info" failed with error code 1 ...
- IndexError: list index out of range的错误原因
第1种可能情况list[index]index超出范围 第2种可能情况list是一个空的 没有一个元素进行list[0]就会出现该错误 ————————————————版权声明:本文为CSDN博主「m ...
- Mac安装Mysql-python遇到的坑,被这俩报错反复摩擦:'my_config.h' file not found 和 IndexError: string index out of range
最后Stackoverflow上面的大神解决了问题: Link brew install mysql brew unlink mysql brew install mysql-connector-c ...
随机推荐
- vray学习笔记(3)-多维子材质是个什么东西
多维子材质是个什么东西?为什么出现这个概念? 在3dsmax官方网站,我们可以看到它的定义: The Multi/Sub-Object material lets you assign differe ...
- kaggle Cross-Validation
The Cross-Validation Procedure In cross-validation, we run our modeling process on different subsets ...
- 手把手教Android商业项目-即时通讯-i美聊
[课程概况] 手把手教你从无到有的完整实现一个Android商业项目,是目前整个市场上所没有的课程,废话不多说,请往下看. [项目概况] 项目名称:i美聊 所属领域:移动社交 即时通讯 代码行数: ...
- ArcGIS Server GP服务使用常见错误总结
ArcGIS GP服务问题列表 输入参数错误 在使用GP服务时,从创建模型到发布服务,再到调用服务,整个过程都需要注意输入参数和输出参数的问题.GP服务支持的输入和输出参数可详见 http://hel ...
- UWP&WP8.1 基础控件—Button
Button作为最常用的控件,没有特别难的用法,是一个非常简单,可以很快就掌握的控件. Button 基础用法: 同样,在UWP项目中,可以从工具箱中拖拽到面板中进行使用.也可以使用XAML语法进行编 ...
- 使用jenkins来跑docker image的惨痛经历
记录一下我使用jenkins来跑docker container的艰辛路程吧,是照着jenkins官网的[文档](https://jenkins.io/doc/tutorials/build-a-ja ...
- hdu1402(大数a*b&fft模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 给出两个长度1e5以内的大数a, b, 输出 a * b. 思路: fft模板 详情参 ...
- 洛谷P4462 [CQOI2018]异或序列(莫队)
打广告->[这里](https://www.cnblogs.com/bztMinamoto/p/9538115.html) 我蠢了…… 如果$a_{l} xor ...a_{r}=k$,那么只要 ...
- Ceph配置文件查看修改方式
1. 修改ceph配置文件的方式有三种:(其中包含临时和永久生效) 1) 修改所有或者指定的进程 2) 修改当前服务器进程 3) 修改配置文件 Note:在线修改 ...
- Python翻译器
import urllib.request import urllib.parse import json content='有了我,翻译再也不是问题啦' print(content) while c ...