python - 列表:索引超出范围
问题描述
这是我的代码:
if __name__ == '__main__':
n = int(input())
for i in range(n):
name = input()
score = float(input())
python_students = [[name, score]]
z=len(python_students)
for i in range(z):
if python_students[i][1]<python_students[i+1][1]:
list = [python_students[1]]
list.sort()
print(list)
error : Traceback (most recent call last):
File "solution.py", line 9, in <module>
if python_students[i][1]<python_students[i+1][1]:
IndexError: list index out of range
我真的对这种类型的错误感到困惑,请解释并帮助我处理这段代码。
我正在尝试从列表中按字母顺序获取名称
解决方案
z
应该是len(python_students)-1
。在最后一次迭代中,python_students[i+1][1]
超出了列表的范围。
推荐阅读
- jquery - 想办法让下拉菜单在 wordpress 中工作
- arrays - 如何根据特定条件安排一个月的任务?
- json - 在 JSON IBM Watson Assistant 中计算年龄
- swift - 离开增强现实 ViewController 时,按钮在设备旋转之前不会检测到触摸
- python - python将日期转换为日期整数
- php - 图片上的链接在手机上不起作用
- excel - 如何解决 CATIA 宏中的运行时错误 438?
- java - 矩阵乘法 Big O 表示法
- c++ - 我的 Eclipse CDT 项目中有一个图像文件。如何在我的代码中引用它,以便协作者在运行代码时可以使用它?
- c - NULL 替代 C 语言 99 标准中的双精度数