Lecture 05 – Dictionaries.  Consider the following methods of sorting information in a sequence. What is the output produced by each program? 2COMPSCI.

Slides:



Advertisements
Similar presentations
Lecture 07 – Iterating through a list of numbers.
Advertisements

Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Dictionaries: Keeping track of pairs
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Fundamentals of Python: From First Programs Through Data Structures
9 Dictionaries © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
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 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
Sequences The range function returns a sequence
COMPSCI 101 Principles of Programming Lecture 24 – Using dictionaries to manage a small file of information.
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
Lecture 05 – Classes.  A class provides the definition for the type of an object  Classes can store information in variables  Classes can provide methods.
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
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.
Introduction. 2COMPSCI Computer Science Fundamentals.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
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.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Built-in Data Structures in Python An Introduction.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
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.
Chapter 9 Dictionaries and Sets.
Computer Science 112 Fundamentals of Programming II Implementation Strategies for Unordered Collections.
Lecture 04 – Models of memory Mutable and immutable data.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Python Data Structures CMSC 201. Built in Types Today we will be talking about some other built in types in python! Tuples Sets Dictionaries.
Lists COMPSCI 105 S Principles of Computer Science.
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Introduction to Python Aug 22, 2013 Hee-gook Jun.
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.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Dictionaries. The compound types you have learned about - - strings, lists, and tuples – use integers as indices. If you try to use any other type as.
Python Sets and Dictionaries Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Set properties What is a set? A set is a mutable.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
Intro to CS Nov 21, 2016.
COMPSCI 107 Computer Science Fundamentals
COMPSCI 107 Computer Science Fundamentals
10 - Python Dictionary John R. Woodward.
Python Variable Types.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
Department of Computer Science,
Announcements Project 4 due Wed., Nov 7
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
CHAPTER THREE Sequences.
CS190/295 Programming in Python for Life Sciences: Lecture 6
4. sequence data type Rocky K. C. Chang 16 September 2018
Introduction to Dictionaries
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Dictionaries Dictionary: object that stores a collection of data
15-110: Principles of Computing
Dictionary.
Presentation transcript:

Lecture 05 – Dictionaries

 Consider the following methods of sorting information in a sequence. What is the output produced by each program? 2COMPSCI Computer Science Fundamentals x = [4, 2, 3, 1] y = x z = sorted(x) print(x, y, z) x = [4, 2, 3, 1] y = x z = x.sort() print(x, y, z)

 An unordered set of key : value pairs  keys must be unique (within a given dictionary)  keys must be immutable 3COMPSCI Computer Science Fundamentals my_dict = { 'alux001' : 'Andrew', 'afer023' : 'Adriana', 'rshe001' : 'Robert' } >>> my_dict['alux001'] 'Andrew' >>> my_dict['rshe001'] 'Robert' >>> my_dict['afer023'] 'Adriana'

 Variables are created when you assign a value for the first time  Assigning a value to a new dictionary entry creates that entry  Assigning a value to an existing dictionary entry changes that entry 4COMPSCI Computer Science Fundamentals my_dict = {} my_dict['alux001'] = 'Andrew' my_dict['rshe001'] = 'Robert' my_dict['afer023'] = 'Adriana' my_dict['alux001'] = 'Andrew Luxton-Reilly'

 Use the del operator  Use the pop method 5COMPSCI Computer Science Fundamentals del my_dict['alux001'] my_dict.pop('alux001')

 Order of items in a dictionary is undefined  Can get a "view" of the keys, values, or (key, value) pairs  Can iterate through the "view"  Views are dynamic, so they change when the dictionary changes 6COMPSCI Computer Science Fundamentals for k in my_dict.keys(): print(k) for v in my_dict.values(): print(v) for i in my_dict.items(): print(i)

 Get a value associated with a given key  Use the index, use get, or use get with default value 7COMPSCI Computer Science Fundamentals >>> my_dict['alux001'] 'Andrew' >>> my_dict['ALUX999'] KeyError: 'ALUX999' >>> my_dict.get('alux001') 'Andrew' >>> my_dict.get('ALUX999') >>> >>> my_dict.get('alux001', 'Not valid') 'Andrew' >>> my_dict.get('ALUX999', 'Not valid') 'Not valid'

 Write a function that accepts a string as an argument and prints out a frequency table of all the characters in the string, along with their frequency. The table should be sorted in order of the characters. 8COMPSCI Computer Science Fundamentals >>> frequency('this is a short sentence') 4 a 1 c 1 e 3 h 2 i 2 n 2 o 1 r 1 s 4 t 3

 A tuple is a sequence, much like a list  A tuple is immutable  Enclosed in brackets (x, y) 9COMPSCI Computer Science Fundamentals >>> point = (1, 2) >>> point (1, 2) >>> point[0] 1 >>> point + (3, 4) (1, 2, 3, 4)

10COMPSCI Computer Science Fundamentals def translate(point, vector): """Translate a point by a given vector Arguments: point – a tuple representing an (x, y) point on the cartesian plane vector – a tuple representing a translation of (dx, dy) on the cartesian plane >>> translate((0, 0), (4, 5)) (4, 5) """ x = point[0] + vector[0] y = point[1] + vector[1] return (x, y) >>> new_x, new_y = translate((1, 2), (3, 4))

 Write a function that calculates the difference between two points and returns a vector (dx, dy) to translate from one to the other. 11COMPSCI Computer Science Fundamentals (x1, y1) (x2, y2) dx) dy

 A sparse matrix is a matrix with few non-zero values.  The most obvious way to represent the matrix is a list of lists: 12COMPSCI Computer Science Fundamentals matrix = [[0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 3, 0, 0, 0], [0, 0, 7, 0, 0, 0, 4], [0, 0, 0, 0, 0, 2, 0] ]

 Alternative implementation  Use a dictionary  Use the location as the key  Store location as a tuple (x, y) 13COMPSCI Computer Science Fundamentals matrix = {(1, 1) : 1, (2, 3) : 7, (3, 2) : 3, (5, 4) : 2, (6, 3) : 4 }

 Use the location (x, y) as the index  Use the location (x, y) as the index, but use a default value 14COMPSCI Computer Science Fundamentals matrix = {(1, 1) : 1, (2, 3) : 7, (3, 2) : 3, (5, 4) : 2, (6, 3) : 4 } >>> matrix[(3, 2)] 3 >>> matrix[(0, 0)] KeyError: (0, 0) >>> matrix.get((4, 3), 0) 0

 Dictionaries are also known as associative arrays or hash tables  Consist of key : value pairs  keys must be immutable  Syntax is {a : b, c : d, …}  Adding an item  d[key] = value  Deleting an item  d.pop(key)  Getting an item  d.get(key, default) 15COMPSCI Computer Science Fundamentals