Outline for February 26, 2018

Reading: §7


  1. Reading a URL [geturl.py,geturl2.py]
    1. Opening a URL
    2. Reading the page as a string
  2. Dictionary
    1. Collection of key-value pairs
  3. Creating dictionaries
    1. Using d = {}
    2. Using d = dict()
  4. 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
  5. Example: memos
    1. Remember how slowly the recursive Fibonacci number program rfib.py ran? Here is a faster recursive Fibonacci [rfibmemo.py]
  6. Sorting the dictionary
    1. sorted sorts based on keys
  7. Example: word frequency count
    1. Unsorted [wfc-1.py]
    2. Sorted alphabetically [wfc-2.py]
    3. Sorted alphabetically, but dictionary order (note key=str.lower() in sorted [wfc-2a.py]
    4. Sorted by frequency (treat lambda x: x[1] as an idiom to reference the value of the dictionary entry, not the key—to go from highest to lowest, replace x[1] with -x[1]) [wfc-3.py]
    5. Sorted by frequency first, then alphabetically—note use of function alphafreq(x); you can use any function here, and the parameter is the item [wfc-4.py]

Matt Bishop
Department of Computer Science
University of California at Davis
Davis, CA 95616-8562 USA
Last modified: Version of February 25, 2018 at 6:40PM
Winter Quarter 2018
You can get a PDF version of this