Permalink
Browse files

dict tutorials

  • Loading branch information...
1 parent b90caa6 commit 3afbcf904a855431d8bb89be7e0aa4d48aa2b0af @ctb ctb committed Jun 15, 2012
Showing with 535 additions and 0 deletions.
  1. +355 −0 ngs-61-intro-to-dicts.ipynb
  2. +180 −0 ngs-62-screed-database-as-dict.ipynb
View
@@ -0,0 +1,355 @@
+{
+ "metadata": {
+ "name": "ngs-61-intro-to-dicts"
+ },
+ "nbformat": 3,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Goal: take some data from counting birds, and ask questions of it.",
+ "",
+ "In particular, how many birds did we see of each type?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ "s = \"\"\"",
+ "bluejay",
+ "goldfinch",
+ "goldfinch",
+ "bald eagle",
+ "ostrich",
+ "bluejay",
+ "goose",
+ "goose",
+ "goose",
+ "goose",
+ "goose",
+ "goose",
+ "goose",
+ "duck",
+ "\"\"\""
+ ],
+ "language": "python",
+ "outputs": [],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "First, let's transform this text list into a \"real\" python list."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ "birdlist = s.split('\\n')",
+ "birdlist = [ x for x in birdlist if x ]",
+ "birdlist"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 6,
+ "text": [
+ "['bluejay',",
+ " 'goldfinch',",
+ " 'goldfinch',",
+ " 'bald eagle',",
+ " 'ostrich',",
+ " 'bluejay',",
+ " 'goose',",
+ " 'goose',",
+ " 'goose',",
+ " 'goose',",
+ " 'goose',",
+ " 'goose',",
+ " 'goose',",
+ " 'duck']"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ "countlist = []",
+ "for bird in birdlist:",
+ " newcountlist = []",
+ " found = False",
+ " for (j, count) in countlist:",
+ " if j == bird:",
+ " found = True",
+ " count += 1",
+ " newcountlist.append((j, count))",
+ " if not found:",
+ " newcountlist.append((bird, 1))",
+ " countlist = newcountlist",
+ " ",
+ "countlist"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 9,
+ "text": [
+ "[('bluejay', 2),",
+ " ('goldfinch', 2),",
+ " ('bald eagle', 1),",
+ " ('ostrich', 1),",
+ " ('goose', 7),",
+ " ('duck', 1)]"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Let's use a dictionary instead"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ "birddict = {}",
+ "# set the value associated with the key 'goose' to the number 7",
+ "birddict['goose'] = 7",
+ "# get the value associated with the key 'goose'",
+ "print birddict['goose']"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7",
+ ""
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ "birddict = {}",
+ "for bird in birdlist:",
+ " birddict[bird] = 0",
+ " ",
+ "for bird in birdlist:",
+ " birddict[bird] = birddict[bird] + 1",
+ "",
+ "birddict"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 14,
+ "text": [
+ "{'bald eagle': 1,",
+ " 'bluejay': 2,",
+ " 'duck': 1,",
+ " 'goldfinch': 2,",
+ " 'goose': 7,",
+ " 'ostrich': 1}"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ "birddict = {}",
+ "",
+ "for bird in birdlist:",
+ " if bird not in birddict:",
+ " birddict[bird] = 1",
+ " else:",
+ " birddict[bird] = birddict[bird] + 1",
+ "",
+ "birddict"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 17,
+ "text": [
+ "{'bald eagle': 1,",
+ " 'bluejay': 2,",
+ " 'duck': 1,",
+ " 'goldfinch': 2,",
+ " 'goose': 7,",
+ " 'ostrich': 1}"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ "birddict = {}",
+ "",
+ "for bird in birdlist:",
+ " birddict[bird] = birddict.get(bird, 0) + 1",
+ "",
+ "birddict"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 18,
+ "text": [
+ "{'bald eagle': 1,",
+ " 'bluejay': 2,",
+ " 'duck': 1,",
+ " 'goldfinch': 2,",
+ " 'goose': 7,",
+ " 'ostrich': 1}"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ "birddict.keys()"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 19,
+ "text": [
+ "['bald eagle', 'bluejay', 'ostrich', 'goose', 'goldfinch', 'duck']"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ "birddict.values()"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 20,
+ "text": [
+ "[1, 2, 1, 7, 2, 1]"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ "birddict.items()"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 21,
+ "text": [
+ "[('bald eagle', 1),",
+ " ('bluejay', 2),",
+ " ('ostrich', 1),",
+ " ('goose', 7),",
+ " ('goldfinch', 2),",
+ " ('duck', 1)]"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ "x = sorted(birddict.items())",
+ "x"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 23,
+ "text": [
+ "[('bald eagle', 1),",
+ " ('bluejay', 2),",
+ " ('duck', 1),",
+ " ('goldfinch', 2),",
+ " ('goose', 7),",
+ " ('ostrich', 1)]"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ "def get_count(a):",
+ " return a[1]",
+ "",
+ "# Sort by count, not by bird name; use get_count to get the count.",
+ "x = sorted(birddict.items(), key=get_count)",
+ "# by default sorted goes from least to most; reverse that.",
+ "x.reverse()",
+ "x"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 33,
+ "text": [
+ "[('goose', 7),",
+ " ('goldfinch', 2),",
+ " ('bluejay', 2),",
+ " ('duck', 1),",
+ " ('ostrich', 1),",
+ " ('bald eagle', 1)]"
+ ]
+ }
+ ],
+ "prompt_number": 33
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ ""
+ ],
+ "language": "python",
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "input": [
+ ""
+ ],
+ "language": "python",
+ "outputs": []
+ }
+ ]
+ }
+ ]
+}
Oops, something went wrong.

0 comments on commit 3afbcf9

Please sign in to comment.