首页 > 解决方案 > getting a student name from the matrix

问题描述

I am learning python and working on assignment to get a new_list of student names from a student_grades list, which is in the form of matrix.

I was able to get it to the point where I got the first name from the list but can't figure out how to complete the loop so it iterates over every name on the matrix. Thank you.

student_grades = [
    ['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
    ['Jane', '100', '90', '80'],
    ['Susan', '88', '99', '111'],
    ['Dick', '45', '56', '67']
]
student_list = []
for names in student_grades:
    #print(names)
    student_list.append(student_grades[1][0])

print(student_list)

['Jane', 'Jane', 'Jane', 'Jane']

标签: pythonlist

解决方案


student_list.append[names[0]]

The problem is that you're currently grabbing exactly the same list element on each iteration, without regard to the iteration.

Also, there's no real reason to force the column headers into your grade book -- let that be a print add-on detail when you print your data in that particular format.

student_grades = [
    ['Jane', '100', '90', '80'],
    ['Susan', '88', '99', '111'],
    ['Dick', '45', '56', '67']
]
student_list = []
for names in student_grades:
    student_list.append(names[0])

print(student_list)

Output:

['Jane', 'Susan', 'Dick']

Finally, I expect that you'll want to keep the scores as integers, rather than strings.


推荐阅读