EECS 110: Lec 13: Dictionaries Aleksandar Kuzmanovic Northwestern University
EECS 110 Today Today: Next week: Python's objects & Classes filesdictionaries Hw 5 Due this Sunday, 5/16
More Practice with Mutation What are the values of A, B, C and D at the indicated points? def mystery1(L1, N, C): for i in L1: if i == N: C += 1 return C def mystery2(C): for i in range(len(C)): C[i] *= 2 >>> A = [22, 10, 30] >>> B = 22 >>> C = 0 >>> D = mystery1(A, B, C) >>> mystery2(A) 1 2 1) 2)
More Practice with Mutation What are the values of A, B, C and D at the indicated points? def mystery1(L1, N, C): for i in L1: if i == N: C += 1 return C def mystery2(C): for i in range(len(C)): C[i] *= 2 >>> A = [22, 10, 30] >>> B = 22 >>> C = 0 >>> D = mystery1(A, B, C) >>> mystery2(A) >>> A 1 1) 2) A = [22, 10, 30] B = 22 C = 0 D = 1 2
More Practice with Mutation What are the values of A, B, C and D at the indicated points? def mystery1(L1, N, C): for i in L1: if i == N: C += 1 return C def mystery2(C): for i in range(len(C)): C[i] *= 2 >>> A = [22, 10, 30] >>> B = 22 >>> C = 0 >>> D = mystery1(A, B, C) >>> mystery2(A) >>> A 1 2 1) 2) A = [22, 10, 30] B = 22 C = 0 D = 1 A = [44, 20, 60]
Name that author… ?
HW5 Pr 2: Read some text and automatically generate new (reasonable?) text
WMSCI 2005 Randomly-generated submission accepted to WMSCI No end to the WMSCI s…
Markov Model The text file: I like spam. I like toast and spam. I eat ben and jerry's ice cream too. Technique for modeling any sequence of natural data Each item depends on only the item immediately before it. The Model: 1st-order Markov Model For each word, keep track of the words that can follow it (and how often) I: like, like, eat like: spam, toast spam.: $ $: I, I, I toast: and eat: ben and: spam, jerry's ben: and jerry's: ice ice: cream cream: too. too.: $ We can repeat words to indicate frequency $ indicates beginning of a sentence
Generative Markov Model Technique for modeling any sequence of natural data Each item depends on only the item immediately before it. A key benefit is that the model can generate feasible data! I like spam. I like spam. I like toast and jerry's ice cream too. Generating text: 1) start with the '$' string 2) choose a word following '$', at random. Call it w 3) choose a word following w, at random. And so on… 4) If w ends a sentence, '$' becomes the next word.
HW5 Pr 2: Need to be able to… Read text from a file Compute and store the model Generate the new text
Reading Files >>> f = file( 'a.txt' ) >>> text = f.read() >>> text 'This is a file.\nLine 2\nLast line!\n' >>> f.close() In Python reading files is no problem…
Files >>> f = file( 'a.txt' ) >>> text = f.read() >>> text 'This is a file.\nLine 2\nLast line!\n' >>> f.close() In Python reading files is no problem… opens the file and calls it f reads the whole file and calls it text text is a single string containing all the text in the file closes the file (closing Python does the same) But how to process the text from here…?
String Manupulation >>> text 'This is a file.\nLine 2\nLast line!\n' >>> print text This is a file. Line 2 Last line! >>> text.split() ['This', 'is', 'a', 'file.', 'Line', '2', 'Last', 'line!'] >>> text 'This is a file.\nLine 2\nLast line!\n' >>> lines = text.split('\n') >>> lines ['This is a file.', 'Line 2', 'Last line!', ''] Returns a list of the words in the string (splitting at spaces, tabs and newlines) Returns a list of the lines in the string (splitting at newlines)
Objects, objects, everywhere! >>> L = [] # create a list, L >>> dir(L) # see all of L's methods >>> help(L.sort) # I wonder… all list methods appear -- lots of them including append, index, remove, and sort >>> help(L.index) # What does this do?
List methodsString methods but there's a fundamental difference… what and why? append count extend index insert pop remove reverse sort capitalize center count find index isalpha lower replace split strip title upper and ~10 more… >>> dir([]) help can help >>> dir('')
Mutable vs. immutable objects Lists are mutable objects. Strings are immutable objects. (So are numbers.) >>> L = [2,1,3] >>> L.sort() >>> L [1,2,3] L has changed. >>> s = 'string' >>> s.replace('st','') 'ring' >>> s 'string' returns a NEW string no return value s has NOT changed
HW5 Pr 2: Need to be able to… Read text from a file Compute and store the model Generate the new text
Lists vs. Dictionaries Lists are not perfect… L L[0]L[1] reference 5 42
Lists vs. Dictionaries Lists are not perfect… You can't choose what to name data. L[0], L[1], … L L[0]L[1] reference 5 42
Lists vs. Dictionaries Lists are not perfect… L[1988] = 'dragon' You can't choose what to name data. You have to start at 0. L[0], L[1], … L L[0]L[1] reference 5 42 L[1989] = 'snake'
Lists vs. Dictionaries Lists are not perfect… L[1988] = 'dragon' You can't choose what to name data. You have to start at 0. Some operations can be slow for big lists … L[0], L[1], … L L[0]L[1] reference 5 42 L[1989] = 'snake' if 'dragon' in L:
Lists vs. Dictionaries In Python a dictionary is a set of key - value pairs. It's a list where the index can be any immutable-type key. >>> d = {} >>> d[1988] = 'dragon' >>> d[1989] = 'snake' >>> d {1988: 'dragon', 1989: 'snake'} >>> d[1988] 'dragon' >>> d[1987] key error
Lists vs. Dictionaries In Python a dictionary is a set of key - value pairs. It's a list where the index can be any immutable-type key. >>> d = {} >>> d[1988] = 'dragon' >>> d[1989] = 'snake' >>> d {1988: 'dragon', 1989: 'snake'} >>> d[1988] 'dragon' >>> d[1987] key error creates an empty dictionary, d 1988 is the key 'dragon' is the value 1989 is the key 'snake' is the value Anyone seen this before? Retrieve data as with lists… or almost !
More on dictionaries Dictionaries have lots of built-in methods: >>> d = {1988: 'dragon', 1989: 'snake'} >>> d.keys() [ 1989, 1988 ] >>> d.has_key( 1988 ) True >>> d.has_key( 1969 ) False >>> d.pop( 1988 ) 'dragon' delete a key (and its value) check if a key is present get all keys
A family dictionary?
A family dictionary… T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer' :['hugo','bart','lisa','maggie'], 'marge' :['hugo','bart','lisa','maggie']} keys can be any immutable type values can be any type at all… T['abe'] How to get 'selma' from T ?
A family dictionary… T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer' :['hugo','bart','lisa','maggie'], 'marge' :['hugo','bart','lisa','maggie']} keys can be any immutable type values can be any type at all… T['abe'] How to get 'selma' from T ? ['homer','herb']
A family dictionary… T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer' :['hugo','bart','lisa','maggie'], 'marge' :['hugo','bart','lisa','maggie']} keys can be any immutable type values can be any type at all… T['abe'] How to get 'selma' from T ? ['homer','herb'] T['jackie'][2] (T['jackie'] is a list)
A functional family? def favChild( person, Tree ): """ person is a name (a string) Tree is a dictionary of children returns person's favorite child """ if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0] else: return 'no children' Who is favored ? Side effects ?
A functional family? def addChild( person, Tree, jr ): """ adds person's new child to Tree ""“ For example, >>> addChild( 'lisa', T, 'abejr' )
A functional family? def addChild( person, Tree, jr ): """ adds person's new child to Tree ""“ if Tree.has_key(person): kids = Tree[person] kids += [jr] For example, >>> addChild( 'lisa', T, 'abejr' )
A challenge… def provinceChallenge( prov ): """ prov is a dictionary of Canada's provinces -- the challenge is to name them all! """ while 0 in prov.values(): guess = raw_input("Name a province: ") if prov.has_key( guess ) == False: print 'Try again...' elif prov[guess] == 0: print 'Yes!' prov[guess] += 1 else: print 'Already guessed...' print 'Phew!' prov = { 'BC': 0, 'AB': 0, … } help?!
“Quiz” Change this code so that it keeps track of how many times you've guessed each item. Based on favChild, write favGChild to return the first grandchild alphabetically - or return 'no one' if there are none. def provinceChallenge( prov ): while 0 in prov.values(): guess = raw_input("Guess: ") if prov.has_key( guess ) == False: print 'Try again...' elif prov[guess] == 0: print 'Yes!' prov[guess] += 1 else: print 'Already guessed...' def favChild( person, Tree ): if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0] else: return 'no children' def favGChild( person, Tree ): first, for real provinces then, for incorrect guesses…
Change this code so that it tells you how many times you've guessed the same province… def provinceChallenge( prov ): while 0 in prov.values(): guess = raw_input("Guess: ") if prov.has_key( guess ) == False: print 'Try again... ' elif prov[guess] == 0: print 'Yes!' prov[guess] += 1 else: print 'Already guessed...‘ first, for real provinces then, for incorrect guesses…
Change this code so that it tells you how many times you've guessed the same province… def provinceChallenge( prov ): prov['incorrect']=0 while '0' in prov.values(): guess = raw_input("Guess: ") if prov.has_key( guess ) == False: print 'Try again... ' prov['incorrect'] += 1 elif prov[guess] == 0: print 'Yes!' prov[guess] += 1 else: print 'Already guessed...‘ prov[guess] += 1 first, for real provinces then, for incorrect guesses…
Based on favChild, write favGChild to return the first grandchild alphabetically - or return 'no one' if there are none. def favChild( person, Tree ): if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0] else: return 'no children'
Based on favChild, write favGChild to return the first grandchild alphabetically - or return 'no one' if there are none. def favGChild( person, Tree ): gChildren = [] if Tree.has_key( person ): for child in Tree[person]: if Tree.has_key( child ): gChildren += Tree[child] if gChildren == []: return 'no one' else: gChildren.sort() return gChildren[0] def favChild( person, Tree ): if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0] else: return 'no children'
Markov Model { 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], 'ben' : ['and'], 'I' : ['like', 'like', 'eat'], '$' : ['I', 'I', 'I'], The text file: I like spam. I like toast and spam. I eat ben and jerry's ice cream too. Technique for modeling any sequence of natural data Each item depends on only the item immediately before it. The Model: