2016-12-23 1 views
-1
나는 다양한 모듈에서 학생들의 점수를 대표 할 테이블을 만들 필요가

파이썬에서 보고서 테이블 만들기. 나는 다음과 같은 표현하고 생각하고 있었는데 : 예를 들어중첩 된 목록

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]) 

내가 조금이 제대로 그래서 나중에 행의 평균을 계산 할 수있는 테이블을 만드는 방법을 혼란 스러워요 , 기둥, 자국 및 학생 보고서 인쇄. 알고리즘을 수정하여 정상적인 테이블을 만들 수있는 방법이 있습니까?

답변

0

A "표"는 작업에 가장 적합한 도구가 될하지 않을 수 있습니다,하지만 우리는 각 학생에 대한 새로운 목록을 시작할 확인하고,의의 표시에 대한 목록을 만들어 보자, 시작하기, 필요한 곳에 추가 할 수 있습니다. 이제 모든 것을 -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 

이제는 이름으로 학생을 검색 할 수있는 사전 같은 대안이 있습니다. 또는 심지어 defaultdict.