Computing Science 1P PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Computing Science 1P


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.
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"
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 ") mDatamonth data
print displays the whole dictionary, and len
gives the size.
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?
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

7
Using dictionaries
A natural idea is to represent the address, phone
number etc. by a list. So John Smiths data might
be
"23 High Street, Sometown", "A12 3PQ", "01234
567890"
If the variable js has been given this value, then
js0 is John Smiths address js1 is John
Smiths postcode js2 is John Smiths phone
number
The program will contain 0, 1, 2 easy to forget.
8
Using dictionaries
A nice alternative is to use a dictionary to
represent each persons details. John Smiths
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 Smiths address js"postcod
e" is John Smiths postcode js"phone" is
John Smiths phone number
which is more readable.
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".
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.
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.
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.
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 dont know what the order will
be.
mData "Jan"12, "Feb"10, "Mar"5, for
month in mData print month, mDatamonth
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,
mDatamonth
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, mDatamonth
This prints the data in alphabetical order of
months strange, but for addresses it might be
more useful!
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 d0, d1
What happens if we use the sort method of
data? Try it!
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
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.
Write a Comment
User Comments (0)
About PowerShow.com