{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Watch [Brandon Rhodes](http://pyvideo.org/speaker/brandon-rhodes.html)' \n", "[A Python Æsthetic: Beauty and Why I Python](http://pyvideo.org/pycon-ca-2012/a-python-sthetic-beauty-and-why-i-python.html)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Smith CS101\n", "Johnson EE201\n", "Williams PH131\n", "Smith PH132\n", "Jones PH131\n", "Brown EE201\n", "Williams CS101\n" ] } ], "source": [ "for line in open('semester.txt'):\n", " print(line, end='')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Williams Sophomore\n", "Johnson Junior\n", "Smith Freshman\n", "Jones Sophomore\n", "Brown Freshman\n" ] } ], "source": [ "for line in open('students.txt'):\n", " print(line, end='')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'Brown': 'EE201',\n", " 'Johnson': 'EE201',\n", " 'Jones': 'PH131',\n", " 'Smith': 'PH132',\n", " 'Williams': 'CS101'}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "semester = dict(line.split() for line in open('semester.txt'))\n", "semester" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(('Smith', 'CS101'),\n", " ('Johnson', 'EE201'),\n", " ('Williams', 'PH131'),\n", " ('Smith', 'PH132'),\n", " ('Jones', 'PH131'),\n", " ('Brown', 'EE201'),\n", " ('Williams', 'CS101'))" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "semester = tuple(tuple(line.split()) for line in open('semester.txt'))\n", "semester" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from collections import defaultdict, Counter" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "defaultdict(list,\n", " {'Brown': ['EE201'],\n", " 'Johnson': ['EE201'],\n", " 'Jones': ['PH131'],\n", " 'Smith': ['CS101', 'PH132'],\n", " 'Williams': ['PH131', 'CS101']})" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "courses_of_student = defaultdict(list)\n", "for line in open('semester.txt'):\n", " name, course = line.split()\n", " courses_of_student[name] += [course]\n", "courses_of_student" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'Brown': 'Freshman',\n", " 'Johnson': 'Junior',\n", " 'Jones': 'Sophomore',\n", " 'Smith': 'Freshman',\n", " 'Williams': 'Sophomore'}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_of_student = dict(line.split() for line in open('students.txt'))\n", "year_of_student" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'Brown', 'Johnson', 'Jones', 'Smith', 'Williams'}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "keys = courses_of_student.keys() & year_of_student.keys()\n", "keys" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "defaultdict(collections.Counter,\n", " {'Freshman': Counter({'CS101': 1, 'EE201': 1, 'PH132': 1}),\n", " 'Junior': Counter({'EE201': 1}),\n", " 'Sophomore': Counter({'CS101': 1, 'PH131': 2})})" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "courses_of_year = defaultdict(Counter)\n", "for student in courses_of_student.keys() & year_of_student.keys():\n", " courses = courses_of_student[student]\n", " year = year_of_student[student]\n", " courses_of_year[year].update(courses)\n", "courses_of_year" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "defaultdict(collections.Counter,\n", " {'CS101': Counter({'Freshman': 1, 'Sophomore': 1}),\n", " 'EE201': Counter({'Freshman': 1, 'Junior': 1}),\n", " 'PH131': Counter({'Sophomore': 2}),\n", " 'PH132': Counter({'Freshman': 1})})" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "years_of_courses = defaultdict(Counter)\n", "for student in courses_of_student.keys() & year_of_student.keys():\n", " courses = courses_of_student[student]\n", " year = year_of_student[student]\n", " for course in courses:\n", " years_of_courses[course].update([year])\n", "years_of_courses" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above was written before I saw how Brandon had written his code." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "defaultdict(collections.Counter,\n", " {'Freshman': Counter({'CS101': 1, 'EE201': 1, 'PH132': 1}),\n", " 'Junior': Counter({'EE201': 1}),\n", " 'Sophomore': Counter({'CS101': 1, 'PH131': 2})})" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "courses_of_year = defaultdict(Counter)\n", "for student in courses_of_student.keys() & year_of_student.keys():\n", " courses = courses_of_student[student]\n", " year = year_of_student[student]\n", " for course in courses:\n", " courses_of_year[year][course] +=1\n", "courses_of_year" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "defaultdict(collections.Counter,\n", " {'CS101': Counter({'Freshman': 1, 'Sophomore': 1}),\n", " 'EE201': Counter({'Freshman': 1, 'Junior': 1}),\n", " 'PH131': Counter({'Sophomore': 2}),\n", " 'PH132': Counter({'Freshman': 1})})" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "years_of_courses = defaultdict(Counter)\n", "for student in courses_of_student.keys() & year_of_student.keys():\n", " courses = courses_of_student[student]\n", " year = year_of_student[student]\n", " for course in courses:\n", " years_of_courses[course][year] += 1\n", "years_of_courses" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }