-7

How would I approach this problem, I am to create a dictionary, where the name of the individual is a key and the values are a tuple made of list of marks(out of 10) and a list of Grade.

So, I have in a text file:

Josh

8  A
8  A
6  C
7  B
9  A

Pablo

7  A
9  B  
8  A
9  B
9  B

output of that should be {Josh:([8,8,6,7,9],['A','A','C','B','A']), Pablo:([7,9,8,9,9],['A','B','A','B','B'])}

This is what i have so far:

def course_grading(student_file):

f = open('student.txt','r')

for line in f:
   new_line = line.strip('\n').split()

Any ideas?

Alex Rayo
  • 3
  • 3
  • 1
    Possible duplicate of http://stackoverflow.com/questions/8424942/creating-a-new-dict-in-python – user3282276 Nov 25 '14 at 19:42
  • 4
    What did you try to implement this? Which programming problems have you met during the implementation? – Eugene Naydenov Nov 25 '14 at 19:42
  • I tried to split it at the white space using .split('/t') but i dont know how to create a tuple of two list with first list of marks and second of grades – Alex Rayo Nov 25 '14 at 19:44

1 Answers1

0

According your question and your comment, I assume you have no problem with the text and input. So the point is how to split up mark and grade?

You can use zip() to do this

# This is an example for Josh, assuming you already have list call lines,
# lines = [(mark1, grad1), (mark2, grade2)......]
marks, grades = zip(*lines) # marks is a tuple of marks, grades is a tuple of grades.

There you go

By the way, the split() function will automatically strip '\n'

ChesterL
  • 347
  • 1
  • 3
  • 11
  • I have a question though, In a text file, how would I go about distinguishing a line with the students name from a line with students marks and grades? – Alex Rayo Nov 25 '14 at 20:14
  • @AlexRayo If you are expecting 'Josh' or 'Pablo', just add an `if` statement, otherwise, you could use 'if ord(line[0]) > 57 or ord(line[0]) < 48` it returns true if the first character is not a digit. `ord()` is a built-in function to see ASCII code of a character – ChesterL Nov 25 '14 at 20:20