2014-05-06 Lab

posted May 6, 2014, 5:16 AM by Samuel Konstantinovich   [ updated May 6, 2014, 5:16 AM ]
DO NOW: (5 minutes)
Get a book from project gutenberg (Alice in wonderland)

Run your program from yesterday on that to see the results! You should strip away the header/footer that project gutenberg adds to the book. Compare your results with your neighbors. 

1. Can dictionaries be reversed so that the keys and values are swapped:  { key1:value1 , key2:value2 ... }  becomes {value1:key1 , value2:key2 ...}. Why or why not? 

2. Make functions to do the following:
makeSquareDict(a,b) a and b are integers such that a<=b. The function will return a dictionary with keys starting at a , ending at but not including b. The values of the items are the squares of the keys.  so:
makeDict(3,7) -> { 3:9, 4:16, 5:25, 6:36}
makeDict(4,4) -> { }

3. Test your function with 5 different examples. 
Make sure you include negative numbers, as well as a case when a=b.

4. Your goal is to implement a function to invert a dictionary. Before you do this decide what to do with duplicate values. We will discuss this before you should write it. 

Notes (You should know most of this already):
I. What is a python Dictionary? 

Dictionaries are containers like lists, but they store pairs of information called items. An item is a pair that contains a key and a corresponding valueDictionaries are not ordered. There is no 1st or 2nd item.
Note inverting isn't reversible because duplicates need to be handled in a special way. 

II. Example of the syntax to create a dictionary is as follows:

    dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

'Alice':'2341' is the first item of this dictionary. 
'Alice' is the key, 
'2341' is the value that corresponds to the key 'Alice'.

Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.

Think of keys as a really awesome index for a super flexible list. Since they act like an index Keys must be unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings or numbers.

III. Example of syntax to get values from a dictionary or put them there:
we can access the values of a dictionary by placing the key into square braces much like the index of a list:

e = {'I':1 , 'II':2 , 'III':3, 'IIV':3 , 'IV':4, 'V':5, 'VI':6}
#now we try e[key]  will get the value associated with the key provided
print e['I']  -> prints 1
print e['IV'] -> prints 4
print e[2] -> causes an error, there is no key 2.

#we can assign keys values
e['X'] = 10

#or re-assign them
e['I'] = 0.999

As you can see there are some similarities to lists. 

Remember keys must be unique but values can repeat:
d = {"cat": 3, "dog":5, "cow":3, "frog": 3, "moose":1, "bird":5}
print d  
# prints:  {'moose': 1, 'cow': 3, 'dog': 5, 'frog': 3, 'cat': 3, 'bird': 5}

#As you can see that 3 can appear more than once

#make some changes to the animal list (d)
d['fish'] = 3 
d['cow'] = 4
#cow cannot appear more than once, so this changes the value paired with cow to 4

print d 
#this would print: {'moose': 1, 'cow': 4, 'fish': 3, 'dog': 5, 'frog': 3, 'cat': 3, 'bird': 5}

IV. Methods of Dictionaries: (Assume D is a dictionary)

The len function works on dictionaries.  len(D) evaluates to the number of items in D.

D.keys() - returns a list of all the keys in D

D.values() - returns a list of all the values in D

D.items() - returns a list of all the items in D

key in D - returns True if the key exists in D False otherwise. Useful to use before you try to get a value. 

D.pop(key) - works as you would expect, removes the key:value from the dictionary, and returns the value. If the key is not found, an error occurs.

but it has an extra feature:
D.pop(key,default_value) - Same as above except: If the key is not found, return the default_value. This is like the  -1 with find in a string, except you specify the special value yourself. 

You can read a more complete list of methods here: http://docs.python.org/2/library/stdtypes.html#mapping-types-dict