In [1]:
for line in open('semester.txt'):
    print(line, end='')


Smith       CS101
Johnson     EE201
Williams    PH131
Smith       PH132
Jones       PH131
Brown       EE201
Williams    CS101

In [2]:
for line in open('students.txt'):
    print(line, end='')


Williams    Sophomore
Johnson     Junior
Smith       Freshman
Jones       Sophomore
Brown       Freshman

In [3]:
semester = dict(line.split() for line in open('semester.txt'))
semester


Out[3]:
{'Brown': 'EE201',
 'Johnson': 'EE201',
 'Jones': 'PH131',
 'Smith': 'PH132',
 'Williams': 'CS101'}

In [4]:
semester = tuple(tuple(line.split()) for line in open('semester.txt'))
semester


Out[4]:
(('Smith', 'CS101'),
 ('Johnson', 'EE201'),
 ('Williams', 'PH131'),
 ('Smith', 'PH132'),
 ('Jones', 'PH131'),
 ('Brown', 'EE201'),
 ('Williams', 'CS101'))

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]:
defaultdict(list,
            {'Brown': ['EE201'],
             'Johnson': ['EE201'],
             'Jones': ['PH131'],
             'Smith': ['CS101', 'PH132'],
             'Williams': ['PH131', 'CS101']})

In [7]:
year_of_student = dict(line.split() for line in open('students.txt'))
year_of_student


Out[7]:
{'Brown': 'Freshman',
 'Johnson': 'Junior',
 'Jones': 'Sophomore',
 'Smith': 'Freshman',
 'Williams': 'Sophomore'}

In [8]:
keys = courses_of_student.keys() & year_of_student.keys()
keys


Out[8]:
{'Brown', 'Johnson', 'Jones', 'Smith', 'Williams'}

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]:
defaultdict(collections.Counter,
            {'Freshman': Counter({'CS101': 1, 'EE201': 1, 'PH132': 1}),
             'Junior': Counter({'EE201': 1}),
             'Sophomore': Counter({'CS101': 1, 'PH131': 2})})

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]:
defaultdict(collections.Counter,
            {'CS101': Counter({'Freshman': 1, 'Sophomore': 1}),
             'EE201': Counter({'Freshman': 1, 'Junior': 1}),
             'PH131': Counter({'Sophomore': 2}),
             'PH132': Counter({'Freshman': 1})})

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]:
defaultdict(collections.Counter,
            {'Freshman': Counter({'CS101': 1, 'EE201': 1, 'PH132': 1}),
             'Junior': Counter({'EE201': 1}),
             'Sophomore': Counter({'CS101': 1, 'PH131': 2})})

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]:
defaultdict(collections.Counter,
            {'CS101': Counter({'Freshman': 1, 'Sophomore': 1}),
             'EE201': Counter({'Freshman': 1, 'Junior': 1}),
             'PH131': Counter({'Sophomore': 2}),
             'PH132': Counter({'Freshman': 1})})