I am trying to insert an element into the first column of each row in a table of data read from a text file.
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
The text file has two columns, one is a four digit code that must be broken down into proper indices for the position in the 2d list. The other is a grade. Each row is a student's grades, whose name I want to insert at the beginning of the list at column 0 of each row.
I thought grades[row][0] = student[s]
was the legal way to insert an item into a list, but I keep getting the error "IndexError: list index out of range" for this line. Anything is appreciated.
You need to use for loop using index rather then comprehension
for i in range(len(inputList()):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.