1 / 18

Computing Science 1P

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], … ].

Download Presentation

Computing Science 1P

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computing Science 1P Lecture 14: Friday 2nd February Simon Gay Department of Computing Science University of Glasgow 2006/07

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

More Related