0

我需要一个二维列表来存储有关学生及其成绩的信息。

当我运行我的程序时,我只得到一份数字列表,但我需要为每个学生单独列出。谁能帮我?

这是我到目前为止所做的:

COLS= int(input("number of students to enter "))
ROWS= int(input("number of grades per student "))

def main():
    number =[]


    for c in range(COLS):
        student =(input("enter student ID number "))
        number.append(student)

        for r in range (ROWS):
            grades =(input("Enter grade for module: "))
            number.append(grades)

    print(number)

my result is 
number of students to enter 2
number of grades per student 4
enter student ID number 1234
Enter grade for module: 55
Enter grade for module: 66
Enter grade for module: 43
Enter grade for module: 33
enter student ID number 2345
Enter grade for module: 34
Enter grade for module: 56
Enter grade for module: 78
Enter grade for module: 99
['1234', '55', '66', '43', '33', '2345', '34', '56', '78', '99']
>>> 
4

5 回答 5

1

我会使用按学生 ID 索引的字典:

COLS= int(input("number of students to enter "))
ROWS= int(input("number of grades per student "))

def main():
    student_grades = {}

    for c in range(COLS):
        student =(input("enter student ID number "))
        grades = []

        for r in range (ROWS):
            grade =(input("Enter grade for module: "))
            grades.append(grade)
        student_grades[student] = grades
于 2013-02-24T18:09:55.583 回答
1

您需要为每一行创建一个列表:

for c in range(COLS):
    grades = []
    student =(input("enter student ID number "))
    number.append(student)
    number.append(grades)

    for r in range (ROWS):
        grade =(input("Enter grade for module: "))
        grades.append(grade)
于 2013-02-24T17:58:50.657 回答
1

根据我的评论,使用list & dict comprehensions执行此操作的示例:

cols = int(input("Number of students: "))
rows = int(input("Number of grades per student: "))

grades = {input("Enter student ID number: "): 
             [input("Enter grade for module: ") for _ in range(rows)] 
          for _ in range(cols)}

请注意,这是Kyle Strand 的答案中的字典,而不是列表。这更适合数据,并且将使以后更容易使用它。

另请注意,可以通过重复此操作来实现更好的界面,直到用户决定不输入更多学生,而不是预先询问将输入多少学生:

rows = int(input("Number of grades per student: "))

def get_students():
    while True:
        value = input("Enter student ID number, or nothing to finish: ")
        if not vale:
            return
        else:
            yield value

grades = {student: [input("Enter grade for module: ") for _ in range(rows)] 
          for student in get_students()}

在这里,这是通过生成器实现的,该生成器生成从用户那里获得的新学生编号,直到用户不输入任何内容。

于 2013-02-24T18:16:57.600 回答
0

只需在将其插入列表之前构建一个number列表:

for c in range(COLS):
    student =(input("enter student ID number "))
    temp_arr = [student] # make a temporary array for the student id and their grades

    for r in range (ROWS):
        grades =(input("Enter grade for module: "))
        temp.append(grades) # append to the temp array here

    # after you are done getting the grades
    # insert the entire temp array into the number array
    number.append(temp)

此外,如果您创建一个类,该数据可能会以更好的方式存储,该类Student可以有一个id和一个grades.

于 2013-02-24T17:59:12.173 回答
0

我认为使用列表字典而不是二维列表(列表列表)会更好,因为它使对数据的访问变得更加容易和可读。出于同样的原因,我还将其名称从number您的代码中更改grades为以下代码中的名称,以更好地反映其中的内容。

COLS = int(input("number of students: "))
ROWS = int(input("number of grades per student: "))

grades = {}

def main():
    for c in range(COLS):
        student = int(input("enter student ID: "))
        grades[student] = []  # initialize to empty row of grades

        for r in range(ROWS):
            grade = int(input("Enter grade for module: "))
            grades[student].append(grade)

    for student in grades.keys():
        print 'student:{}, grades:{}'.format(student, grades[student])

输入样本数据后的输出:

student:2345, grades:[34, 56, 78, 99]
student:1234, grades:[55, 66, 43, 33]
于 2013-02-24T22:46:12.210 回答