python - 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']
解决方案
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.
推荐阅读
- type-conversion - 如何在 Crystal 中将字符串转换为整数或浮点数?
- vba - VBA查找值并将其放在特定列中
- python - 使用 LIKE (pyqt5) 在 mysql 中选择语句
- ios - UISearchController 显示问题 - iPhone X 和其他设备
- php - 雄辩 | 使用 with() 导致返回 NULL
- php - 在 PHP 中从字符串中的内容创建一个数组
- keras - 如何在 keras 中为一维数据(pca/zca 处理后的数据)建立模型?
- sql - 如何根据特定条件在 SQL 中将行分成列?
- grails - Grails:如何将 @BindUsing 用于带有 belongsTo 的属性
- firebase - Angular 6 Web 应用程序中推送通知的 FCM 设置