python - 无法在python中插入二维列表
问题描述
我正在尝试将一个元素插入从文本文件读取的数据表中每一行的第一列。
grades = [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],
[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],
[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]
student = ["Alice Arnold", "Cory Brown", "Sean Douglas", "Pete Douglas", "Mary Fleming", "Joel Jacob",
"Jeramy Ghouse", "Hansie Holder", "Loyola Ingenious", "Gary Jackson", "Russell Jacobson",
"Dustan Kellart", "Carl Melone" , "Samuel Peterson", "Alec Stewart"]
code = ""
s=0
for i in file:
pos = 9
code = file.read(pos)
info = code.split("\n")
indexRow = info.pop()
row = int(indexRow[0:2]) #slice the first 2 digits for row index
col = int(indexRow[2:4]) #slice the other 2 digits for the column index
gr = float(indexRow[5:9]) #slice the grade
grades[row][col] = gr
grades[row][0] = student[s]
s+=1
文本文件有两列,一列是四位代码,必须分解为二维列表中位置的适当索引。另一个是等级。每行是一个学生的成绩,我想在每行第 0 列的列表开头插入他的名字。
我认为grades[row][0] = student[s]
这是将项目插入列表的合法方式,但我一直收到此行的错误“IndexError:列表索引超出范围”。任何事情都值得赞赏。
解决方案
您需要使用索引而不是理解来使用 for 循环
for i in range(len(inputList()):
推荐阅读
- reactjs - 使用数组解构时保留挂钩类型
- c# - 从数据表中删除列
- excel - Excel循环比较两列
- python - 训练 vgg16 时,训练和验证曲线的损失和准确率会出现峰值
- firebase - 用于删除 Firebase 云消息订阅的 GUI
- angular - 当我在引导程序中使用导航栏下拉菜单时,我在 Angular 中不能有多个点击事件
- java - 如何在 char 数组中存储零而不是存储 null [JAVA]
- tensorflow - 有时会收到错误“err == cudaSuccess || err == cudaErrorInvalidValue Unexpected CUDA error: out of memory”
- python - Django 3.0 Not Found: /favicon.ico and failing to store uploaded image into disk
- nginx - Minikube Ingress 无法解析,但 minikube IP 可以