compsci 6 introduction to computer science november 1, 2011 prof. rodger
TRANSCRIPT
![Page 1: CompSci 6 Introduction to Computer Science November 1, 2011 Prof. Rodger](https://reader036.vdocuments.net/reader036/viewer/2022082819/56649f1f5503460f94c37e15/html5/thumbnails/1.jpg)
CompSci 6Introduction to Computer Science
November 1, 2011
Prof. Rodger
![Page 2: CompSci 6 Introduction to Computer Science November 1, 2011 Prof. Rodger](https://reader036.vdocuments.net/reader036/viewer/2022082819/56649f1f5503460f94c37e15/html5/thumbnails/2.jpg)
Announcements• No Reading for next time, no RQ
• Apt-04 out, due Thursday
• Today– Writing Dictionaries/maps– Solving one apt EmailsCourse
![Page 3: CompSci 6 Introduction to Computer Science November 1, 2011 Prof. Rodger](https://reader036.vdocuments.net/reader036/viewer/2022082819/56649f1f5503460f94c37e15/html5/thumbnails/3.jpg)
Dictionaries/Maps
• Dictionaries/maps are another way of organizing data
• Keys and Values– Each key maps to a value– Some keys can map to the same value– Can change the value a key maps to
![Page 4: CompSci 6 Introduction to Computer Science November 1, 2011 Prof. Rodger](https://reader036.vdocuments.net/reader036/viewer/2022082819/56649f1f5503460f94c37e15/html5/thumbnails/4.jpg)
Example• Each student could be mapped to their
favorite ice cream flavor
![Page 5: CompSci 6 Introduction to Computer Science November 1, 2011 Prof. Rodger](https://reader036.vdocuments.net/reader036/viewer/2022082819/56649f1f5503460f94c37e15/html5/thumbnails/5.jpg)
Implementing a Dictionary/MapKeys map to values
• Create Empty dictionarysomemap = {}
• Put in a key and its valuesomemap[“Forbes”] = “Strawberry”
• Get a value for a dictionaryvalue = somemap[“Forbes”]
OR value = somemap.get(“Forbes”, “default”)
• Change a value for a dictionarysomemap[“Forbes’] = “Chocolate”
![Page 6: CompSci 6 Introduction to Computer Science November 1, 2011 Prof. Rodger](https://reader036.vdocuments.net/reader036/viewer/2022082819/56649f1f5503460f94c37e15/html5/thumbnails/6.jpg)
More on using a Dictionary/Map• Get all the keys
– listKeys = somemap.keys()
• Get all the values– listValues = somemap.values()
• Other methods– clear – empty dictionary– items – return (key,value) pairs– Iteritems – return (key,value) pairs more
efficiently– update – update with another dictionary
![Page 7: CompSci 6 Introduction to Computer Science November 1, 2011 Prof. Rodger](https://reader036.vdocuments.net/reader036/viewer/2022082819/56649f1f5503460f94c37e15/html5/thumbnails/7.jpg)
Change Astrachan’s valuesomemap[“Astrachan”] = Coffee Mocha
![Page 8: CompSci 6 Introduction to Computer Science November 1, 2011 Prof. Rodger](https://reader036.vdocuments.net/reader036/viewer/2022082819/56649f1f5503460f94c37e15/html5/thumbnails/8.jpg)
Value could be a set or list
![Page 9: CompSci 6 Introduction to Computer Science November 1, 2011 Prof. Rodger](https://reader036.vdocuments.net/reader036/viewer/2022082819/56649f1f5503460f94c37e15/html5/thumbnails/9.jpg)
Back to Popular Name Problem:• Given a list of names, determine the most
popular first name and print that name with all of its last names.
• Input: Names are always two words, names are in a file. If multiple names are on the same line they are separated by a “:”
• Output: Most popular first name, followed by a “:”, followed by corresponding last names separated by a blank
![Page 10: CompSci 6 Introduction to Computer Science November 1, 2011 Prof. Rodger](https://reader036.vdocuments.net/reader036/viewer/2022082819/56649f1f5503460f94c37e15/html5/thumbnails/10.jpg)
Now use a dictionary/map
• We will write three maps for practice– First name to count of corresponding last names– First name to list of corresponding last names– First name to set of corresponding last names
• Which map is most useful to solve this problem?
![Page 11: CompSci 6 Introduction to Computer Science November 1, 2011 Prof. Rodger](https://reader036.vdocuments.net/reader036/viewer/2022082819/56649f1f5503460f94c37e15/html5/thumbnails/11.jpg)
Compare
• Using two parallel lists?
• Using one dictionary/map