180 likes | 298 Views
Computing Science 1P. Lecture 14: Friday 2 nd February. Simon Gay Department of Computing Science University of Glasgow. 2006/07. Dictionaries. Recall the idea of using nested lists to represent labelled data:. record = [ ["Jan",12], ["Feb",10], ["Mar",5], … ].
E N D
Computing Science 1P Lecture 14: Friday 2nd February Simon Gay Department of Computing Science University of Glasgow 2006/07
Dictionaries Recall the idea of using nested lists to represent labelled data: record = [ ["Jan",12], ["Feb",10], ["Mar",5], … ] Quite attractive, but there are problems, mainly that finding an item requires searching from the beginning of the list. Storing a collection of values indexed by keys is very common, so Python provides the dictionary to make it easier. Computing Science 1P Lecture 14 - Simon Gay
Dictionaries A dictionary can be created simply by listing the contents: mData = {"Jan":12, "Feb":10, "Mar":5, … } and then we can find the value for a given key: mData["Feb"] gives 10 It is also possible to update and remove items, in the same way as with lists. mData["Mar"] = 15 del mData["Dec"] Computing Science 1P Lecture 14 - Simon Gay
Dictionaries Often dictionaries are used for large collections of data, which may be calculated or obtained from another source. Example: entering monthly data from the keyboard mData = {} for i in range(12): month = raw_input("Enter month: ") data = input("Enter data: ") mData[month] = data print displays the whole dictionary, and len gives the size. Computing Science 1P Lecture 14 - Simon Gay
Using dictionaries Imagine that we want to represent an address book. It contains data (address, phone number) indexed by name. An obvious idea is to use a dictionary in which the keys are the names. { "John Smith": …, "Anne Brown": …, "Henry Jones": …, … } What should the values be? Computing Science 1P Lecture 14 - Simon Gay
How about using a dictionary for the addresses and another dictionary for the phone number? • Good idea • Not a good idea • Don't know Computing Science 1P Lecture 14 - Simon Gay
Using dictionaries A natural idea is to represent the address, phone number etc. by a list. So John Smith’s data might be [ "23 High Street, Sometown", "A12 3PQ", "01234 567890" ] If the variable js has been given this value, then js[0] is John Smith’s address js[1] is John Smith’s postcode js[2] is John Smith’s phone number The program will contain 0, 1, 2: easy to forget. Computing Science 1P Lecture 14 - Simon Gay
Using dictionaries A nice alternative is to use a dictionary to represent each person’s details. John Smith’s data would be { "address":"23 High Street, Sometown", "postcode":"A12 3PQ", "phone":"01234 567890" } If the variable js has been given this value, then js["address"] is John Smith’s address js["postcode"] is John Smith’s postcode js["phone"] is John Smith’s phone number which is more readable. Computing Science 1P Lecture 14 - Simon Gay
Aside In Python a dictionary is implemented by means of a standard data structure called a hash table. The main feature of a hash table is that arbitrary items can be found efficiently – more efficiently than searching from the beginning of a list. If you are interested, do a Google search for "hash table". Computing Science 1P Lecture 14 - Simon Gay
Dictionary methods To find all of the keys in a dictionary: mData.keys() gives [ "Jan", "Feb", "Mar", "Apr", … ] but not necessarily in this order. keys is called a method of mData. A method is similar to a function, but it is associated with a particular object (in this case mData). So keys does not need to be given any arguments to tell it which dictionary we are interested in. Computing Science 1P Lecture 14 - Simon Gay
Aside If you know about object-oriented programming then note that the words object and method on the previous slide should be understood in that sense. We will see many more examples of methods in Python, and eventually we will look at methods and objects more systematically. For now, we just need to note that many of the standard operations on Python data structures are expressed as methods rather than functions. Computing Science 1P Lecture 14 - Simon Gay
Dictionary methods To find all of the values in a dictionary: mData.values() gives [ 5, 10, 12, … ] but not necessarily in this order. The method items returns the keys and the values, as a list of tuples: mData.items() gives [ ("Jan",12), ("Feb",10), ("Mar",5), … ] but not necessarily in this order. Computing Science 1P Lecture 14 - Simon Gay
Iterating over a dictionary Sometimes we want to do some processing for every item in a dictionary. Example: print the whole address book. An easy way is to use for, which iterates over the keys, but we don’t know what the order will be. mData = {"Jan":12, "Feb":10, "Mar":5, … } for month in mData: print month, mData[month] Computing Science 1P Lecture 14 - Simon Gay
Iterating over a dictionary Another way is to get the list of keys and iterate over that: mData = {"Jan":12, "Feb":10, "Mar":5, … } months = mData.keys() for month in months: print month, mData[month] Computing Science 1P Lecture 14 - Simon Gay
Iterating over a dictionary The sort method of lists can be useful (p76 of the book): mData = {"Jan":12, "Feb":10, "Mar":5, … } months = mData.keys() months.sort() for month in months: print month, mData[month] This prints the data in alphabetical order of months: strange, but for addresses it might be more useful! Computing Science 1P Lecture 14 - Simon Gay
Iterating over a dictionary Alternatively we can iterate over the list of items, remembering that they are tuples: mData = {"Jan":12, "Feb":10, "Mar":5, … } data = mData.items() for d in data: print d[0], d[1] What happens if we use the sort method of data? Try it! Computing Science 1P Lecture 14 - Simon Gay
Other useful dictionary methods The method has_key returns True if the given key is present in the dictionary, False otherwise. mData = {"Jan":12, "Feb":10, "Mar":5, … } mData.has_key("Feb") returns True mData.has_key("Sunday") returns False Computing Science 1P Lecture 14 - Simon Gay
Other useful dictionary methods The method get looks up a key, and allows a default return value to be specified in case the key is not present. mData = {"Jan":12, "Feb":10, "Mar":5, … } mData.get("Feb",-1) returns 10 mData.get("Sunday",-1) returns -1 See Section 10.7 of the book for an example related to recent exercise sheets. Computing Science 1P Lecture 14 - Simon Gay