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

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Course A201: Introduction to Programming 10/28/2010.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lecture 05 – Dictionaries.  Consider the following methods of sorting information in a sequence. What is the output produced by each program? 2COMPSCI.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
CSC1016 Coursework Clarification Derek Mortimer March 2010.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Computing Science 1P Large Group Tutorial 17 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Computing Science 1P Lecture 18: Friday 2 nd March Simon Gay Department of Computing Science University of Glasgow 2006/07.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
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.
If statements while loop for loop
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computer Science 1000 Spreadsheets V Permission to redistribute or use these slides is strictly prohibited without permission.
Computing Science 1P Lecture 21: Wednesday 18 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Neal Stublen Computer Memory (Simplified)  Remember, all programming decisions came down to a true or false evaluation  Consider.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Word Create a basic TOC. Course contents Overview: table of contents basics Lesson 1: About tables of contents Lesson 2: Format your table of contents.
Classwork: Common Errors Primary keys: don’t forget them! Primary keys: choose the best one! – “Name” and “birthday” are not the best choices. – “Phone.
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.
1 i206: Lecture 12: Hash Tables (Dictionaries); Intro to Recursion Marti Hearst Spring 2012.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
Maps Nick Mouriski.
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.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Python Programing: An Introduction to Computer Science
JavaScript and Ajax (JavaScript Arrays) Week 5 Web site:
Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
String and Lists Dr. José M. Reyes Álamo.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
Containers and Lists CIS 40 – Introduction to Programming in Python
Department of Computer Science,
LING 388: Computers and Language
File Handling Programming Guides.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
String and Lists Dr. José M. Reyes Álamo.
Remembering lists of values lists
Introduction to Dictionaries
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Introduction to Programming with Python
Sample lecture slides.
Tuple.
Introduction to Computer Science
Presentation transcript:

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

Computing Science 1P Lecture 14 - Simon Gay2 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 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.

2006/07Computing Science 1P Lecture 14 - Simon Gay3 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"]

2006/07Computing Science 1P Lecture 14 - Simon Gay4 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.

2006/07Computing Science 1P Lecture 14 - Simon Gay5 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?

2006/07Computing Science 1P Lecture 14 - Simon Gay6 How about using a dictionary for the addresses and another dictionary for the phone number? Good idea Not a good idea Don't know

2006/07Computing Science 1P Lecture 14 - Simon Gay7 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", " " ] 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.

2006/07Computing Science 1P Lecture 14 - Simon Gay8 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":" " } 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.

2006/07Computing Science 1P Lecture 14 - Simon Gay9 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".

2006/07Computing Science 1P Lecture 14 - Simon Gay10 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.

2006/07Computing Science 1P Lecture 14 - Simon Gay11 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.

2006/07Computing Science 1P Lecture 14 - Simon Gay12 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.

2006/07Computing Science 1P Lecture 14 - Simon Gay13 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]

2006/07Computing Science 1P Lecture 14 - Simon Gay14 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]

2006/07Computing Science 1P Lecture 14 - Simon Gay15 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!

2006/07Computing Science 1P Lecture 14 - Simon Gay16 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!

2006/07Computing Science 1P Lecture 14 - Simon Gay17 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

2006/07Computing Science 1P Lecture 14 - Simon Gay18 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.