for line in open('semester.txt'):
print(line, end='')
Smith CS101 Johnson EE201 Williams PH131 Smith PH132 Jones PH131 Brown EE201 Williams CS101
for line in open('students.txt'):
print(line, end='')
Williams Sophomore Johnson Junior Smith Freshman Jones Sophomore Brown Freshman
semester = dict(line.split() for line in open('semester.txt'))
semester
{'Brown': 'EE201', 'Johnson': 'EE201', 'Jones': 'PH131', 'Smith': 'PH132', 'Williams': 'CS101'}
semester = tuple(tuple(line.split()) for line in open('semester.txt'))
semester
(('Smith', 'CS101'), ('Johnson', 'EE201'), ('Williams', 'PH131'), ('Smith', 'PH132'), ('Jones', 'PH131'), ('Brown', 'EE201'), ('Williams', 'CS101'))
from collections import defaultdict, Counter
courses_of_student = defaultdict(list)
for line in open('semester.txt'):
name, course = line.split()
courses_of_student[name] += [course]
courses_of_student
defaultdict(list, {'Brown': ['EE201'], 'Johnson': ['EE201'], 'Jones': ['PH131'], 'Smith': ['CS101', 'PH132'], 'Williams': ['PH131', 'CS101']})
year_of_student = dict(line.split() for line in open('students.txt'))
year_of_student
{'Brown': 'Freshman', 'Johnson': 'Junior', 'Jones': 'Sophomore', 'Smith': 'Freshman', 'Williams': 'Sophomore'}
keys = courses_of_student.keys() & year_of_student.keys()
keys
{'Brown', 'Johnson', 'Jones', 'Smith', 'Williams'}
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
defaultdict(collections.Counter, {'Freshman': Counter({'CS101': 1, 'EE201': 1, 'PH132': 1}), 'Junior': Counter({'EE201': 1}), 'Sophomore': Counter({'CS101': 1, 'PH131': 2})})
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
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.
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
defaultdict(collections.Counter, {'Freshman': Counter({'CS101': 1, 'EE201': 1, 'PH132': 1}), 'Junior': Counter({'EE201': 1}), 'Sophomore': Counter({'CS101': 1, 'PH131': 2})})
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
defaultdict(collections.Counter, {'CS101': Counter({'Freshman': 1, 'Sophomore': 1}), 'EE201': Counter({'Freshman': 1, 'Junior': 1}), 'PH131': Counter({'Sophomore': 2}), 'PH132': Counter({'Freshman': 1})})