Michael Ernst CSE 140 University of Washington

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Variables and References in Python. "TAGAGAATTCTA” Objects s Names References >>> s = “TAGAGAATTCTA” >>>
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Dictionaries: Keeping track of pairs
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
COMPSCI 101 Principles of Programming Lecture 24 – Using dictionaries to manage a small file of information.
Data Structures UW CSE 190p Summer >>> xs = range(3) >>> xs = [1,2,3] >>> xs = [‘a’,’b’,’c’] >>> xs = [1, ‘a’, 3] >>> xs = [[1,2,3], [‘a’,’b’,’c’]]
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Built-in Data Structures in Python An Introduction.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Collections Michael Ernst CSE 190p University of Washington.
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.
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
Dictionaries Ruth Anderson CSE 160 University of Washington 1.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON TUPLES, SETS AND DICTIONARIES Jehan-François Pâris
Lists Michael Ernst CSE 140 University of Washington.
Dictionaries Ruth Anderson UW CSE 160 Winter
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Intro to CS Nov 21, 2016.
SQL – Python and Databases
10 - Python Dictionary John R. Woodward.
Sharing, mutability, and immutability
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
Computer Programming Fundamentals
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
When to use Tuples instead of Lists
CSc 120 Introduction to Computer Programing II
Ruth Anderson University of Washington CSE 160 Spring 2015
Section 6: Sequences Chapter 4 and 5.
The Python Data Model UW CSE 190p Summer 2012.
Ruth Anderson CSE 140 University of Washington
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Sharing, mutability, and immutability
Ruth Anderson UW CSE 160 Spring 2018
CISC101 Reminders Quiz 2 this week.
Ruth Anderson UW CSE 160 Winter 2017
Ruth Anderson University of Washington CSE 160 Spring 2018
Sharing, mutability, and immutability
Ruth Anderson University of Washington CSE 160 Winter 2017
Sharing, mutability, and immutability
4. sequence data type Rocky K. C. Chang 16 September 2018
Sharing, mutability, and immutability
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Ruth Anderson UW CSE 160 Winter 2017
Datatypes in Python (Part II)
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Dictionaries Dictionary: object that stores a collection of data
Python Primer 1: Types and Operators
Sharing, mutability, and immutability
Michael Ernst CSE 140 University of Washington
Nate Brunelle Today: Dictionaries
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Dictionaries: Keeping track of pairs
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Python Sets and Dictionaries
Ruth Anderson CSE 160 University of Washington
Ruth Anderson UW CSE 160 Spring 2018
Nate Brunelle Today: Dictionaries
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Nate Brunelle Today: Style, Collections
Introduction to Dictionaries
Sample lecture slides.
Dictionary.
Introduction to Computer Science
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

Michael Ernst CSE 140 University of Washington Dictionaries Michael Ernst CSE 140 University of Washington

Dictionaries or mappings 5 → 25 7 → 49 6 → 36 5 → 25 7 → 49 6 → 36 A dictionary maps each key to a value Order does not matter Given a key, can look up a value Given a value, cannot look up its key No duplicate keys Multiple keys may map to the same value Keys and values are Python values Keys must be immutable (not a list, set, or dict) Can add key → value mappings to a dictionary Can also remove (less common) 7 → 49 -7 → 49 49 → 7 49 → -7 1783 → “Revolutionary” 1848 → “Mexican” 1865 → “Civil” “Revolutionary” → “Mexican” → “Civil” → 1783 1861 1848 1846 1775 1865 “Revolutionary” → “Mexican” → “Civil” → 1783 1861 1848 1846 1775 1865 “WWI” → 1918 1917 add mapping

Dictionary syntax in Python d = dict() us_wars_by_end = { 1783: "Revolutionary", 1848: "Mexican", 1865: "Civil" } us_wars_by_name = { "Civil" : [1861, 1865], "Mexican" : [1846, 1848], "Revolutionary" : [1775, 1783] } Syntax just like arrays, for accessing and setting: us_wars_by_end[1783]  “Revolutionary” us_wars_by_end[1783][1:10]  “evolution” us_wars_by_name["WWI"] = [1917, 1918] 1783 → “Revolutionary” 1848 → “Mexican” 1865 → “Civil” “Revolutionary” → 1775 1783 “Mexican” → 1846 1848 “Civil” → 1861 1865 What are the three questions you want to ask about a data structure? creation, querying, modification

Creating a dictionary “Atlanta” → “GA” “Seattle” → “WA” >>> state = {“Atlanta” : “GA”, “Seattle” : “WA”} >>> phonebook = dict() >>> phonebook[“Alice”] = “206-555-4455” >>> phonebook[“Bob”] = “212-555-2211” >>> atomicnumber = {} >>> atomicnumber[“H”] = 1 >>> atomicnumber[“Fe”] = 26 >>> atomicnumber[“Au”] = 79 “Alice” → “206-555-4455” “Bob” → “212-555-1212” “H” → 1 “Fe” → 26 “Au” → 79

Accessing a dictionary “H” → 1 “Fe” → 26 “Au” → 79 >>> atomicnumber = {“H”:1, “Fe”:26, “Au”:79} >>> atomicnumber["Au"] 79 >>> atomicnumber["B"] Traceback (most recent call last): File "<pyshell#102>", line 1, in <module> atomicnumber["B"] KeyError: 'B' >>> atomicnumber.has_key("B") False >>> atomicnumber.keys() ['H', 'Au', 'Fe'] >>> atomicnumber.values() [1, 79, 26] >>> atomicnumber.items() [('H', 1), ('Au', 79), ('Fe', 26)] Good for iteration (for loops) for key in mymap.keys(): val = mymap[key] … use key and val for key in mymap: for (key,val) in mymap.items():

Modifying a dictionary us_wars1 = { "Revolutionary" : [1775, 1783], "Mexican" : [1846, 1848], "Civil" : [1861, 1865] } us_wars1["WWI"] = [1917, 1918] # add mapping del us_wars_by_name["Mexican"] # remove mapping “Revolutionary” → “Mexican” → “Civil” → 1783 1861 1848 1846 1775 1865 “Revolutionary” → “Mexican” → “Civil” → 1783 1861 1848 1846 1775 1865 “WWI” → 1918 1917 add mapping

Dictionary exercises Convert a list to a dictionary: Given [5, 6, 7], produce {5:25, 6:36, 7:49} Reverse a dictionary: Given {5:25, 6:36, 7:49}, produce {25:5, 36:6, 49:7} What does this do? squares = { 1:1, 2:4, 3:9, 4:16 } squares[3] + squares[3] squares[3 + 3] squares[2] + squares[2] squares[2 + 2]

A list is like a dictionary A list maps an integer to a value The integers must be a continuous range 0..i mylist = [‘a’,’b’,’c’] mylist[1]  ‘b’ mylist[3] = ‘c’ # error! In what ways is a list more convenient than a dictionary? In what ways is a list less convenient than a dictionary?

Not every value is allowed to be a key Keys must be immutable values int, float, bool, string, tuple not: list, set, dictionary Goal: only dictionary operations change the keyset after “mydict[x] = y”, mydict[x]  y if a == b, then mydict[a] == mydict[b] These conditions should hold until mydict is changed Mutable keys can violate these goals list1 = ["a", "b"] list2 = list1 list3 = ["a", "b"] mydict = {} mydict[list1] = "z"  Hypothetical; actually illegal in Python mydict[list3]  "z" list2.append("c") mydict[list1]  ??? mydict[list3]  ??? We will learn about tuples later