Outline for May 18, 2009

Reading: §11.6


  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. D.has_key(k): True if dictionary D has key k; else False
    2. k in D: same as D.has_key(k)
    3. D.keys(): list of keys in D
    4. D.values(): list of values in D
    5. D.items: list of tuples (key, value) in D
    6. D.get(k, d): if key k in D, return associated value; else return d
    7. del D[k]: delete (k, v) from D
    8. D.clear(): delete all entries in D
  4. Sorting the dictionary
    1. keylist.sort() sorts based on keys
    2. keylist.sort(cmpfun) sorts based on cmpfun
      1. Arguments to cmpfun are two (key, value) pairs
      2. Returns −1 if first > second; 0 if first = second; 1 if first < second
  5. Example: word frequency count
    1. Sorted alphabetically (see wfc-1.py)
    2. Sorted by frequency (see wfc-2.py)
    3. Sorted by frequency first, the alphabetically (see wfc-3.py)