9 Dictionaries © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.

Slides:



Advertisements
Similar presentations
4 If-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Advertisements

How SAS implements structured programming constructs
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Dictionaries: Keeping track of pairs
Guide to Programming with Python
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Python Dictionary.
Lecture 05 – Dictionaries.  Consider the following methods of sorting information in a sequence. What is the output produced by each program? 2COMPSCI.
Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.
Sequences The range function returns a sequence
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Python Control of Flow.
 1 String and Data Processing. Sequential Processing Processing each element in a sequence for e in [1,2,3,4]: print e for c in “hello”: print c for.
Tutorial 10 Dictionaries and Classes. Reminder Assignment 09 is due April 6 at 11:55pm (last day of classes) Next tutorial is on April 6 (Monday)
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
10 Recursion © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
8 For-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
Built-in Data Structures in Python An Introduction.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
6 Strings and Tuples © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
11 Map ADTs  Map concepts  Map applications  A map ADT: requirements, contract.  Implementations of maps: using key-indexed arrays, entry arrays, linked-lists,
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
EECS 110: Lec 13: Dictionaries Aleksandar Kuzmanovic Northwestern University
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
11 Map ADTs Map concepts. Map applications.
10 - Python Dictionary John R. Woodward.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Data types: Complex types (Dictionaries)
Lecture 10 Data Collections
CHAPTER THREE Sequences.
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Ruth Anderson UW CSE 160 Winter 2017
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Topics Sequences Introduction to Lists List Slicing
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CS 1111 Introduction to Programming Fall 2018
Dictionaries Dictionary: object that stores a collection of data
Ruth Anderson CSE 160 University of Washington
15-110: Principles of Computing
Topics Sequences Introduction to Lists List Slicing
Ruth Anderson UW CSE 160 Spring 2018
By Himanshi dixit 11th ’B’
Lists [] First = [1, 2, 3, 4] Second = list[range(1,5)]
Dictionary.
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

9 Dictionaries © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1

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.

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

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.

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.

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

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

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

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.

9-11 Example: printing a dictionary (1)  We can simply print a dictionary: print currency  Output: {'UK': 'pound', 'FR': 'euro', 'DE': 'euro'}

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

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.

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.

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, ' ')

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!'

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 '??'

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!'

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