Computing Science 1P

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

description

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

Transcript of Computing Science 1P

Page 1: Computing Science 1P

Computing Science 1P

Lecture 14: Friday 2nd February

Simon GayDepartment of Computing Science

University of Glasgow

2006/07

Page 2: Computing Science 1P

2006/07 Computing Science 1P Lecture 14 - Simon Gay 2

Dictionaries

record = [ ["Jan",12], ["Feb",10], ["Mar",5], … ]

Recall the idea of using nested lists to represent labelled data:

Quite attractive, but there are problems, mainly that finding anitem 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.

Page 3: Computing Science 1P

2006/07 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 sameway as with lists.

mData["Mar"] = 15 del mData["Dec"]

Page 4: Computing Science 1P

2006/07 Computing Science 1P Lecture 14 - Simon Gay 4

Dictionaries

Often dictionaries are used for large collections of data, whichmay 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.

Page 5: Computing Science 1P

2006/07 Computing Science 1P Lecture 14 - Simon Gay 5

Using dictionaries

Imagine that we want to represent an address book. It containsdata (address, phone number) indexed by name.

An obvious idea is to use a dictionary in which the keys are thenames.

{ "John Smith": …, "Anne Brown": …, "Henry Jones": …, … }

What should the values be?

Page 6: Computing Science 1P

2006/07 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

Page 7: Computing Science 1P

2006/07 Computing Science 1P Lecture 14 - Simon Gay 7

Using dictionaries

A natural idea is to represent the address, phone number etc. bya 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 addressjs[1] is John Smith’s postcodejs[2] is John Smith’s phone number

The program will contain 0, 1, 2: easy to forget.

Page 8: Computing Science 1P

2006/07 Computing Science 1P Lecture 14 - Simon Gay 8

Using dictionaries

A nice alternative is to use a dictionary to represent eachperson’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 addressjs["postcode"] is John Smith’s postcodejs["phone"] is John Smith’s phone number

which is more readable.

Page 9: Computing Science 1P

2006/07 Computing Science 1P Lecture 14 - Simon Gay 9

Aside

In Python a dictionary is implemented by means of a standarddata structure called a hash table.

The main feature of a hash table is that arbitrary items can befound efficiently – more efficiently than searching from thebeginning of a list.

If you are interested, do a Google search for "hash table".

Page 10: Computing Science 1P

2006/07 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 aparticular object (in this case mData). So keys does not needto be given any arguments to tell it which dictionary we areinterested in.

Page 11: Computing Science 1P

2006/07 Computing Science 1P Lecture 14 - Simon Gay 11

Aside

If you know about object-oriented programming then note thatthe words object and method on the previous slide should beunderstood in that sense.

We will see many more examples of methods in Python, andeventually we will look at methods and objects moresystematically.

For now, we just need to note that many of the standardoperations on Python data structures are expressed asmethods rather than functions.

Page 12: Computing Science 1P

2006/07 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 oftuples:

mData.items()

gives [ ("Jan",12), ("Feb",10), ("Mar",5), … ]but not necessarily in this order.

Page 13: Computing Science 1P

2006/07 Computing Science 1P Lecture 14 - Simon Gay 13

Iterating over a dictionary

Sometimes we want to do some processing for every item in adictionary. Example: print the whole address book.

An easy way is to use for, which iterates over the keys, but wedon’t know what the order will be.

mData = {"Jan":12, "Feb":10, "Mar":5, … }

for month in mData:print month, mData[month]

Page 14: Computing Science 1P

2006/07 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]

Page 15: Computing Science 1P

2006/07 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!

Page 16: Computing Science 1P

2006/07 Computing Science 1P Lecture 14 - Simon Gay 16

Iterating over a dictionary

Alternatively we can iterate over the list of items, rememberingthat 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!

Page 17: Computing Science 1P

2006/07 Computing Science 1P Lecture 14 - Simon Gay 17

Other useful dictionary methods

The method has_key returns True if the given key is presentin the dictionary, False otherwise.

mData = {"Jan":12, "Feb":10, "Mar":5, … }

mData.has_key("Feb") returns True

mData.has_key("Sunday") returns False

Page 18: Computing Science 1P

2006/07 Computing Science 1P Lecture 14 - Simon Gay 18

Other useful dictionary methods

The method get looks up a key, and allows a default returnvalue 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 recentexercise sheets.