[英]Unable to insert into 2d list in 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
文本文件有兩列,一列是四位代碼,必須將其分解為二維列表中 position 的正確索引。 另一個是等級。 每行是一個學生的成績,我想在每行第 0 列的列表開頭插入他的名字。
我認為grades[row][0] = student[s]
是將項目插入列表的合法方式,但我一直收到此行的錯誤“IndexError:list index out of range”。 任何事情都值得贊賞。
您需要使用索引而不是理解來使用 for 循環
for i in range(len(inputList()):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.