Download presentation
Presentation is loading. Please wait.
Published byLeslie Marjory Booker Modified over 9 years ago
1
9 Dictionaries © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1
2
9-2 Dictionaries (1) A dictionary is a set of (key, value) entries, such that no two entries contain the same key. The entries are in no particular order, so a dictionary is not a sequence. Each key must be of immutable type (e.g., a number, string, or tuple). Each value may be of any type.
3
9-3 Dictionaries (2) A dictionary may be: –homogeneous (all keys are of the same type, and all values are of the same type); or –heterogeneous (keys and/or values are of mixed types).
4
9-4 Dictionaries (3) The following expression constructs an empty dictionary: { }{ } The following expression constructs a non-empty dictionary: { k 1 : v 1, …, k n : v n } where each k i is a key and each v i is a value.
5
9-5 Dictionaries (4) The following expression indexes a dictionary d: d [ k ]d [ k ] where k is a key. This yields the value from the entry with key k. It fails if d contains no such entry. The following statement updates a dictionary d: d [ k ] = v where k is a key and v is a value. This adds a new entry (k, v) to d. If there is an existing entry with key k, it is replaced.
6
9-6 Example: dictionary of Roman numerals Constructing a (homogeneous) dictionary of Roman numerals and their values: roman = {'I': 1, 'V': 5, 'X': 10} Tabular view: roman ‘I’ 1 ‘V’ 5 ‘X’ 10 keys values Indexing the dictionary: roman['I'] yields 1 roman['X'] yields 10
7
9-7 Example: dictionary of currencies (1) Constructing a (homogeneous) dictionary of EU countries and their currencies: currency = \ {'UK': 'pound', 'FR': 'euro', 'DE': 'euro'} Tabular view: currency ‘UK’ ‘pound’ ‘FR’ ‘euro’ ‘DE’ ‘euro’ keys values
8
9-8 Example: dictionary of currencies (2) Indexing the dictionary: currency['UK'] yields ‘pound’ currency['FR'] yields ‘euro’ Updating an entry: currency['UK'] = 'euro' – UK joins the eurozone Adding an entry: currency['IS'] = 'euro' – Iceland joins the EU Removing an entry: del currency['IS'] – Iceland leaves again
9
9-9 Testing a dictionary The expression “d.has_key( k ) ” yields True iff key k is present in dictionary d. E.g.: currency.has_key('UK') – yields True currency.has_key('US') – yields False
10
9-10 Traversing a dictionary The expression “d.keys() ” yields a list of all the keys in dictionary d. E.g.: currency.keys() – yields [‘UK’, ‘FR’, ‘DE’] Note that the keys are in no particular order. To traverse a dictionary, iterate over its keys (e.g., using a for-statement), and use each key to index the dictionary.
11
9-11 Example: printing a dictionary (1) We can simply print a dictionary: print currency Output: {'UK': 'pound', 'FR': 'euro', 'DE': 'euro'}
12
9-12 Example: printing a dictionary (1) To print a dictionary in tabular format: print 'Country', '\t', 'Currency' for country in currency.keys(): print country, '\t', currency[country] Output: Country Currency UK pound FR euro DE euro
13
9-13 Example: student records (1) Consider the set of student records for a particular course (such as AP2). For simplicity, assume that each student record consists of: –a student’s id (which is unique) –the student’s name –the student’s grade.
14
9-14 Example: student records (2) We can model the set of student records as a dictionary. In each dictionary entry: –the key part will be a student id; –the value part will be a (name, grade) pair.
15
9-15 Example: student records (3) Function to enrol a student: def enrol (records, id, name): # Add a new student record (with no grade). # to records. if records.has_key(id): print 'Student ' + id + \ ' already enrolled!' else: records[id] = (name, ' ')
16
9-16 Example: student records (4) Function to award a grade to a student: def award (records, id, grade): # Add a grade to a student record in records. if records.has_key(id): (name, old_grade) = records[id] records[id] = (name, grade) else: print 'Student ' + id + \ ' not enrolled!'
17
9-17 Example: student records (5) Function to retrieve a student’s grade: def course_grade (records, id): # Return the grade of student id in records. if records.has_key(id): (name, grade) = records[id] return grade else: print 'Student ' + id + \ ' not enrolled!' return '??'
18
9-18 Example: student records (6) Function to withdraw a student: def withdraw (records, id): # Remove a student record from records. if records.has_key(id): del records[id] else: print 'Student ' + id + \ ' not enrolled!'
19
9-19 Example: student records (7) Function to tabulate the student records: def tabulate (records): # Tabulate the student records in records. print 'Id', '\t', 'Name', '\t', 'Grade' for id in records.keys(): (name, grade) = records[id] print id + '\t' + name \ + '\t' + grade
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.