In [1]:
for line in open('semester.txt'):
print(line, end='')
In [2]:
for line in open('students.txt'):
print(line, end='')
In [3]:
semester = dict(line.split() for line in open('semester.txt'))
semester
Out[3]:
In [4]:
semester = tuple(tuple(line.split()) for line in open('semester.txt'))
semester
Out[4]:
In [5]:
from collections import defaultdict, Counter
In [6]:
courses_of_student = defaultdict(list)
for line in open('semester.txt'):
name, course = line.split()
courses_of_student[name] += [course]
courses_of_student
Out[6]:
In [7]:
year_of_student = dict(line.split() for line in open('students.txt'))
year_of_student
Out[7]:
In [8]:
keys = courses_of_student.keys() & year_of_student.keys()
keys
Out[8]:
In [9]:
courses_of_year = defaultdict(Counter)
for student in courses_of_student.keys() & year_of_student.keys():
courses = courses_of_student[student]
year = year_of_student[student]
courses_of_year[year].update(courses)
courses_of_year
Out[9]:
In [10]:
years_of_courses = defaultdict(Counter)
for student in courses_of_student.keys() & year_of_student.keys():
courses = courses_of_student[student]
year = year_of_student[student]
for course in courses:
years_of_courses[course].update([year])
years_of_courses
Out[10]:
The above was written before I saw how Brandon had written his code.
In [11]:
courses_of_year = defaultdict(Counter)
for student in courses_of_student.keys() & year_of_student.keys():
courses = courses_of_student[student]
year = year_of_student[student]
for course in courses:
courses_of_year[year][course] +=1
courses_of_year
Out[11]:
In [12]:
years_of_courses = defaultdict(Counter)
for student in courses_of_student.keys() & year_of_student.keys():
courses = courses_of_student[student]
year = year_of_student[student]
for course in courses:
years_of_courses[course][year] += 1
years_of_courses
Out[12]: