我需要一个二维列表来存储有关学生及其成绩的信息。
当我运行我的程序时,我只得到一个数字列表,但我需要为每个学生单独列出。任何人都可以帮助我吗?
这是我到目前为止所做的:
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']
>>>
答案 0 :(得分: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)
答案 1 :(得分: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
答案 2 :(得分: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's answer中的字典,而不是列表。这样可以更好地适应数据,并且以后更容易使用它。
另外请注意,通过重复此操作可以实现更好的界面,直到用户决定不再输入更多学生,而不是直接询问将输入多少学生:
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()}
这是通过一个生成器实现的,它生成从用户获得的新学号,直到用户不输入任何内容。
答案 3 :(得分: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
列表,那么这些数据可能会以更好的方式存储。
答案 4 :(得分: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]