=== 10-19-09 Day 13 (though my numbering of the days is off) ======= Dictionary recap # Define emissions = {1799: 1, 1800: 70, 1801: 74, 1802: 82, 1902: 215630, 2002: 1733297} # Keys can be any immutable object; associated values can be any object d = {'abc': 32, 7: [1, 2, 3], True: 14.6, ("hi", "friend"): 77} # Look up emissions[1802] # Update d[4] = "silly" d[4] = "goofy" d # Check length len(emissions) len(d) - Things you can do with dictionaries: # check for membership 1950 in emissions # remove >>> del emissions[1950] >>> 1950 in emissions False # loop over a dictionary >>> for key in emissions: ... print key >>> for key in emissions: ... print emissions[key] - Dictionaries are unordered. The order that the keys are traversed (when you loop through) is arbitrary: there is no guarantee that it will be in the order that they were added. - dictionaries are mutable. == Dictionary methods == - keys >>> emissions.keys() [1799, 1800, 1801, 1802, 1902, 2002] - values >>> emissions.values() [1, 70, 74, 82, 215630, 1733297] - items: the (key, value) pairs >>> emissions.items() [(1799, 1), (1800, 70), (1801, 74), (1802, 82), (1902, 215630), (2002, 1733297)] # this is a list of tuples - get >>> emissions.get(1802) 82 >>> emissions.get(1805) # returns None >>> emissions.get(1805, -1) # return -1 if the key not there. -1 - update: copies values from one dictionary into another >>> dict1 = {'978-6025': 'Diane', '978-3965': 'Tom'} >>> dict2 = {'978-6360': 'UG office', '978-6025': 'main office'} >>> dict1.update(dict2) # add dict2 contents to dict1 >>> dict1 - clear: removes the dictionary contents >>> dict2 >>> dict2.clear() >>> dict2 == Phone number example: inverting a dictionary == phone = {'555-7632': 'Paul', '555-9832': 'Andrew', '555-6677': 'Jen', '555-9823': 'Michael', '555-6342' : 'Wael', '555-7343' : 'Paul', '555-2222' : 'Michael'} - Get all of Paul's phone numbers: # Method 1 >>> paul = [] >>> for key in phone: ... if phone[key] == 'Paul': ... paul.append(key) ... >>> paul ['555-7632', '555-7343'] - But what if I want to be able to do this for all people? QU: is there some object you could create to make this easy? - an "inverted" dictionary. The old one takes us from numbers to names. The new one will take us in the reverse direction, from names to #s new_phone = {} for (number, name) in phone.items(): if name in new_phone: new_phone[name].append(number) else: new_phone[name] = [number] >>> new_phone {'Jen': ['555-6677'], 'Paul': ['555-7632', '555-7343'], 'Wael': ['555-6342'], 'Michael': ['555-9823', '555-2222'], 'Andrew': ['555-9832']} [inverted_dict.py] - go through this in the debugger. ==== NOW: we will solve a problem from the beginning. == Top-down design == - begins at the top level with what you need the program to do - works its way down to functions that the program need - then to functions that those functions need, and so on. == Writing epics == - We'll develop a program that mimics the writing it has seen - demo [wannabe.py] - Even though the text doesn't make all that much sense, can you see the differences in the style? - After we understand the code, I'll talk about why this type of program is really useful for helping computers to understand human language [predict what you will say or type next!] - QU: How might it work? (a) Remember what you saw so that you can mimic it Type "help", "copyright", "credits" or "license" for more information. >>> # A demo of the program to mimic the writing in a text: >>> Evaluating epic.py upon the sky, So shall behold the humble-bees, And leads me come to be not this cold fruitless moon. TITANIA. Thou shalt buy this advantage take, An I have we grew together, More than arrow from my Thisby's face. Where art thou aby it. What though I pick'd a day As Shafalus to be a fine tragedy. And here together. THESEUS. True; and you were a little voice: 'Thisne, Thisne!' [Then speaking small] 'Ah Pyramus, I'll apply To greet me like to steal. HERMIA. Methinks she was in love you? HELENA. Your vows are to perish on thee! [They sleep] Enter OBERON at the Duke of doubt he That is too old device, and here's a day. The lover, that from his scene of Athens here, But, in the wood Enter DEMETRIUS I'll give a girdle round and lovers seek new ribbons to entreat your patience well. Exit Enter a woman, God warrant us: She shall we come without warning. HIPPOLYTA. How can be set aside. Away with thy brawls thou aby it. Demetrius loves me have a serpent. HERMIA. I see. Dian's bud o'er and not merit. Where is a knavery of man to form in mockery, set. The >>> >>> # The kind of dictionary we want to build: # for midsummernight.txt >>> d = {"happy": ["days", "is", "fair!", "some", "is", "hour!"]} >>> d["couples"] = ["shall","three"] >>> d [Go into midsummernight.txt to find these occurences of the words] >>> >>> # To mimic the writing we've read: suppose we just wrote word w. >>> # Now, random pick the next word from the list of follow-on words for w in >>> # our dictionary. Separate issue: what word to start the whole thing with. >>> >>> # How to pick a random element of a list: >>> inport random >>> >>> l = [5,2,7,33,1,2,66,5] >>> random.shuffle(l) >>> l [2, 66, 2, 1, 33, 7, 5, 5] >>> random.shuffle(l) >>> l [1, 5, 2, 2, 5, 33, 66, 7] Remember d? d