Outline for February 22, 2012

Reading: §20

  1. Dictionary
    1. Collection of key-value pairs
    2. What a “mapping” is
    3. Mutable
  2. Creating dictionaries
    1. Using d = {}
    2. Using d = dict()
  3. Methods for dictionaries
    1. k in D: True if dictionary D has key k; else False
    2. D.keys(): list of keys in D
    3. D.values(): list of values in D
    4. D.items(): list of tuples (key, value) in D
    5. D.get(k, d): if key k in D, return associated value; else return d
    6. del D[k]: delete tuple with key k from D
    7. D.clear(): delete all entries in D
  4. Example: memos
    1. Recursive Fibonacci [rfibmemo.py]
  5. Sorting the dictionary
    1. sorted sorts based on keys
  6. Example: word frequency count
    1. Unsorted [wfc-1.py]
    2. Sorted alphabetically [wfc-2.py]
    3. Sorted alphabetically, but dictionary order [wfc-2a.py]
    4. Sorted by frequency [wfc-3.py]
    5. Sorted by frequency first, the alphabetically [wfc-4.py]

A PDF version is available here.
ECS 10, Basic Concepts of Computing
Winter Quarter 2012