使用嵌套列表在 Python 中创建报告 table
Creating a report table in Python with nested lists
我需要创建一个 table 来表示学生在各个模块中的分数。我正在考虑做以下表示:
Students, a list of strings that are student names. For each i such that
0 <=i <= len(Students) - 1, we refer to Students[i] as the i-th student.
Modules, a list of strings that are modules names. For each i such that
0 <=i <=len(Modules) - 1, we refer to Modules[i] as the i-th module.
Marks a table whose rows correspond to the students and columns correspond to the modules. Marks[i][j] is an integer number defined as follows.
{ If Marks[i][j] = -1 this means that the i-th student is not registered
for the j-th module.
{ If Marks[i][j] =>0 then this is the mark of the i-th student for the
j-th module. We can assume that the marks are in range 0-100,
there is no need to check the validity of the data.
比如我有:
students=['Artur', 'Igor', 'David', 'Andy']
modules=['DM', 'ISD', 'INS', 'IS']
marks=marks[i][j]=int
for i in range(0, len(students)-1) #i ranges over all row numbers
for i in range(0, len(students)-1) #j ranges over all indices
print(a[i][j])
我对如何正确构建一个 table 有点困惑,这样我以后可以计算行、列、标记的平均值并打印学生报告。有没有办法修改算法,使其构建正常的 table?
A "table" 可能不是这项工作的最佳工具,但为了让您开始,让我们为标记制作一个列表,并在需要的地方附加到它,确保我们开始一个新列表每个学生。让我们暂时将所有内容初始化为 -1。
students=['Artur', 'Igor', 'David', 'Andy', 'Fran']
modules=['DM', 'ISD', 'INS', 'IS']
marks = []
for i in range(len(students)):
marks.append([])
for j in range(len(modules)):
marks[i].append(-1)
>>> marks
[[-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1]]
我们现在有一个列表列表。
注意一些事情
- 我添加了额外的学生来显示我们有五个列表,每个列表有四个
模块。
- 该范围不包括最后一个数字,因此您不需要
-1
marks=marks[i][j]=int
没有任何意义。我刚刚做了
一个列表并附加到它。
您现在可以更改模块的分数,并轻松找到平均分。
>>> marks[0][1] = 50
>>> marks
[[-1, 50, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1
, -1, -1]]
>>> for scores in marks:
... print sum(scores)/len(scores)
...
11
-1
-1
-1
-1
现在,有字典等替代方法,可以让您按姓名查找学生。甚至是默认字典。
我需要创建一个 table 来表示学生在各个模块中的分数。我正在考虑做以下表示:
Students, a list of strings that are student names. For each i such that
0 <=i <= len(Students) - 1, we refer to Students[i] as the i-th student.
Modules, a list of strings that are modules names. For each i such that
0 <=i <=len(Modules) - 1, we refer to Modules[i] as the i-th module.
Marks a table whose rows correspond to the students and columns correspond to the modules. Marks[i][j] is an integer number defined as follows.
{ If Marks[i][j] = -1 this means that the i-th student is not registered
for the j-th module.
{ If Marks[i][j] =>0 then this is the mark of the i-th student for the
j-th module. We can assume that the marks are in range 0-100,
there is no need to check the validity of the data.
比如我有:
students=['Artur', 'Igor', 'David', 'Andy']
modules=['DM', 'ISD', 'INS', 'IS']
marks=marks[i][j]=int
for i in range(0, len(students)-1) #i ranges over all row numbers
for i in range(0, len(students)-1) #j ranges over all indices
print(a[i][j])
我对如何正确构建一个 table 有点困惑,这样我以后可以计算行、列、标记的平均值并打印学生报告。有没有办法修改算法,使其构建正常的 table?
A "table" 可能不是这项工作的最佳工具,但为了让您开始,让我们为标记制作一个列表,并在需要的地方附加到它,确保我们开始一个新列表每个学生。让我们暂时将所有内容初始化为 -1。
students=['Artur', 'Igor', 'David', 'Andy', 'Fran']
modules=['DM', 'ISD', 'INS', 'IS']
marks = []
for i in range(len(students)):
marks.append([])
for j in range(len(modules)):
marks[i].append(-1)
>>> marks
[[-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1]]
我们现在有一个列表列表。
注意一些事情
- 我添加了额外的学生来显示我们有五个列表,每个列表有四个 模块。
- 该范围不包括最后一个数字,因此您不需要
-1
marks=marks[i][j]=int
没有任何意义。我刚刚做了 一个列表并附加到它。
您现在可以更改模块的分数,并轻松找到平均分。
>>> marks[0][1] = 50
>>> marks
[[-1, 50, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1
, -1, -1]]
>>> for scores in marks:
... print sum(scores)/len(scores)
...
11
-1
-1
-1
-1
现在,有字典等替代方法,可以让您按姓名查找学生。甚至是默认字典。