Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

Similar documents
MUTABLE LISTS AND DICTIONARIES 4

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

CS Programming I: Arrays

Chapter 8 Dictionaries. Dictionaries are mutable, unordered collections with elements in the form of a key:value pairs that associate keys to values.

List of squares. Program to generate a list containing squares of n integers starting from 0. list. Example. n = 12

CS1 Lecture 11 Feb. 9, 2018

Data Structures. Lists, Tuples, Sets, Dictionaries

Overview.

Collections. Lists, Tuples, Sets, Dictionaries

Notebook. March 30, 2019

LECTURE 3 Python Basics Part 2

Introduction to Python. Fang (Cherry) Liu Ph.D. Scien5fic Compu5ng Consultant PACE GATECH

Programming Languages. Function-Closure Idioms. Adapted from Dan Grossman's PL class, U. of Washington

Dictionaries. By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region. Based on CBSE Curriculum Class -11. Neha Tyagi, KV 5 Jaipur II Shift

COMP1730/COMP6730 Programming for Scientists. Dictionaries and sets

Compound Data Types 1

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

COMP10001 Foundations of Computing Functions

Sequences and iteration in Python

All programs can be represented in terms of sequence, selection and iteration.

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Introduction to Computer Programming for Non-Majors

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Introduction to Python! Lecture 2

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

Data Structures. Dictionaries - stores a series of unsorted key/value pairs that are indexed using the keys and return the value.

Python Intro GIS Week 1. Jake K. Carr

Lecture #12: Mutable Data. map rlist Illustrated (III) map rlist Illustrated. Using Mutability For Construction: map rlist Revisited

ENGR 102 Engineering Lab I - Computation

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

Python Tutorial. CS/CME/BioE/Biophys/BMI 279 Oct. 17, 2017 Rishi Bedi

Topic 7: Lists, Dictionaries and Strings

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Lecture 10: Boolean Expressions

Strings, Lists, and Sequences

Python Lists, Tuples, Dictionaries, and Loops

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

List Processing Patterns and List Comprehension

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

ECSE 321 Assignment 2

Lecture #12: Mutable Data. Last modified: Sun Feb 19 17:15: CS61A: Lecture #12 1

MEIN 50010: Python Data Structures

CIS192 Python Programming

List Processing Patterns and List Comprehension

TUPLES AND RECURSIVE LISTS 5

Programming in Python

Python Programming: Lecture 2 Data Types

Introduction to Bioinformatics

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Advanced Algorithms and Computational Models (module A)

(CC)A-NC 2.5 by Randall Munroe Python

Control Structures 1 / 17

CSc 120 Introduction to Computer Programing II Adapted from slides by Dr. Saumya Debray

Python Lists. What is not a Collection. A List is a kind of Collection. friends = [ 'Joseph', 'Glenn', 'Sally' ]

COMP284 Scripting Languages Lecture 3: Perl (Part 2) Handouts

CIS192: Python Programming

Introduction to Python

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013)

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CMSC201 Computer Science I for Majors

Sharing, mutability, and immutability. Ruth Anderson UW CSE 160 Spring 2018

Lecture 7: Python s Built-in. in Types and Basic Statements

More BSTs & AVL Trees bstdelete

Python Review IPRE

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

CSE 341 Section Handout #6 Cheat Sheet

Introduction to Computer Programming for Non-Majors

CIS192 Python Programming

Python for Everybody. Exploring Data Using Python 3. Charles R. Severance

CMSC 201 Fall 2015 Lab 12 Tuples and Dictionaries

The Practice of Computing Using PYTHON

Python Tutorial. Day 1

Computer Sciences 368 Scripting for CHTC Day 3: Collections Suggested reading: Learning Python

pygtrie Release Jul 03, 2017

Lists, loops and decisions

Chapter 8 SETS AND DICTIONARIES

CMSC201 Computer Science I for Majors

CMPT 120 Lists and Strings. Summer 2012 Instructor: Hassan Khosravi

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

python 01 September 16, 2016

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

More Examples Using Lists Tuples and Dictionaries in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #11

Some material adapted from Upenn cmpe391 slides and other sources

Unit 2. Srinidhi H Asst Professor

CIS192 Python Programming

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

CS 1110 Prelim 1 March 7th, 2013 SOLUTIONS

Basic Python Revision Notes With help from Nitish Mittal

PyTrie Documentation. Release 0.3. George Sakkis

CS 1110 Prelim 1 March 7th, 2013

Announcements. Lecture Agenda. Class Exercise. Hashable. Mutability. COMP10001 Foundations of Computing Iteration

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han

This report is based on sampled data. Jun 1 Jul 6 Aug 10 Sep 14 Oct 19 Nov 23 Dec 28 Feb 1 Mar 8 Apr 12 May 17 Ju

Getting Information from a Table

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Python Review IPRE

Transcription:

Looking up English words in the dictionary Comparing sequences to collections. Sequence : a group of things that come one after the other Collection : a group of (interesting) things brought together for some purpose CS111 Computer Programming Department of Computer Science Wellesley College Is a sequence a collection? Is a collection a sequence? 15-2 Python sequences and collections Definitions and examples of Python types. Properties of sequences and collections Properties that are common and distinct for the two categories. Type Description Example list a mutable sequence of arbitrary objects [-100, "blue", (1, 10), ] tuple an immutable sequence of arbitrary objects (2017, "Mar", 2) string an immutable sequence of characters "Go Wellesley!" set dict an unordered collection of distinct objects. An unordered collection of :value pairs, where s are immutable and values are any Python objects Note: set will be covered in the next lecture. {1, 4, 5, 23} {"orange": "fruit", 3: "March", "even": [2,4,6,8]} Collections (list, tuple, string, set, dict) Find their length with len Check an element membership in the collection with in Are iterables (one can iterate over their elements with a loop) Sequences (list, tuple, string) Use indices to access elements, e.g. mylist[2] Use slice operations to access subsequences, e.g. mylist[2:5] Mutable (list, set, dict): can be changed through object methods. Immutable (tuple, string): cannot be changed. 15-3 15-4

New type: dictionary, its syntax (use { }), and :value pairs. s s: any immutable type such as numbers, strings, or tuples. A Python dictionary is a mutable collection that maps s to values. phones = {5558671234: 'Gal Gadot', 9996541212: 'Trevor Noah', 7811234567: 'Paula A. Johnson'} A dictionary is enclosed with curly brackets and contains commaseparated pairs. A pair is a colon-separated and value. daysofmonth = {'Jan': 31, 'Feb': 28, 'Mar': 31, 'Apr': 30, } value monthlengths = {31: ['Jan','Mar','May','Jul','Aug','Oct','Dec'], 30: ['Apr', 'Jun','Sep','Nov'], 28: ['Feb'] } s cannot mutate Keys can only be numbers, strings, or tuples. 15-5 An analogy to P.O. box s. If the is damaged, one cannot retrieve the content. daysofmonth = {'Jan': 31, 'Feb': 28, 'Mar': 31, 'Apr': 30, 'May': 31, 'Jun': 30, 'Jul': 31, 'Aug': 31, 'Sep': 30, 'Oct': 30, 'Nov': 30} friends = {('Harry','Potter'):['harry@hogwarts.edu','Gryffindor'], ('Cho', 'Chang'):['cho@hogwarts.edu', 'Ravenclaw'], ('Cedric', 'Diggory'):['ced@hogwarts.edu','Hufflepuff']} In daysofmonth, the for December is missing, it will be added later in the slides. values 15-6 Differently from s, a value can be any object. values: any Python object (numbers, strings, lists, tuples, dicts, sets, even functions) = {:, : 'Munger Hall', :, : } functionnames = {'map': [double, square, pluralize], 'filter': [iseven, ispositive, contains] } contributions = {'uma52': {2015: 10, 2016: 15}, 'setam$3': {2012: 23, 2013: 34, 2014: 17}, 'rid12': {2009: 5, 2010: 18, 2012: 4} } 15-7 15-8

How to create dictionaries? 1. Literal dictionary: provide s and pairs together, delimited with { } In [1]: scrabbledict = {'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10} 2. Start with an empty dict and add /pairs In [2]: cart = {} # an empty dict In [3]: cart['oreos'] = 3.99 In [4]: cart['kiwis'] = 2.54 In [5]: cart Out[5]: {'kiwis': 2.54, 'oreos': 3.99} 3. Applying the built-in function dict to a list of tuples: Three common ways to create dictionaries. The output Out[5] doesn t show the items in the dictionary in the same order they were added. Never except that items will be ordered. In [5]: dict([('deu', 49), ('ALB', 355), ('FRA', 33)]) Out[5]: {'ALB': 355, 'DEU': 49, 'FRA': 33} 15-9 Dictionary Operations: subscripting The value associated with a is accessed using the same subscripting notation with square brackets used for list indexing: In [6]: daysofmonth['oct'] Out[6]: 31 In [7]: friends[('harry','potter')] Out[7]: ['harry@hogwarts.edu','gryffindor'] In [8]: phones[5558671234] Out[8]: 'Gal Gadot' In [9]: functionnames['map'][0] Out[9]: double In [10]: contributions['rid12'][2010] Out[10]: 18 list index We use the subscripting notation with (s) to access values. Inputs [9] and [10] use double subscription to access elements that are nested within complex values. In In[9], an element within a list, in In[10] a value within a nested dictionary. 15-10 Dictionary Operations: check with in before accessing Subscripting a dictionary with an invalid raises a KeyError: In [12]: daysofmonth['october'] ------------------------------ KeyError Traceback (most recent call last) <ipython-input-4-3d32324d55ec> in <module>() ----> 1 daysofmonth['october'] KeyError: 'October' Non-existing s raise an error, use the operator in to check if exists. Mutability in are mutable We can add or remove -value pairs. We can change the value associated with a. daysofmonth['feb'] = 29 # change for leap year daysofmonth['dec'] = 31 # add new and value However, dictionary s must be immutable The only immutable things in a dictionary are its s. One way to avoid such errors is to use in to check if a exists In [13]: 'Oct' in daysofmonth Out[13]: In [14]: 'October' in daysofmonth Out[14]: False 15-11 Eg: a list or a dict cannot be a (only immutable values such as numbers, strings, and tuples) 15-12

Memory diagram for a dictionary = {:, : 'Munger Hall', 'section': 2, :, : } are Mutable: change value for The value associated with a can be changed by combining subscript and assignment notation: An assignment statement is used to add new / value pairs or change existing ones. 'sec-on' 2 'Munger Hall' Notice how the s are shown similarly to the indices in sequences. However, they are not ordered in any meaningful way. To remember: Only primitive values such as numbers, booleans, and None are displayed within the slots. Other values (strings, tuples, lists, objects) are shown outside. 14-13 'sec-on' In [21]: [] = 'Cazenove Hall' 2 'Munger Hall' 'Cazenove Hall' Canopy displays dictionaries with string-valued s in ASCII order (see Out[22]). But, this is not true in other environments, thus, don t rely on order or s. In [22]: Out[22]: {:, : 'Cazenove Hall', :, 'section': 2, : } 15-14 are Mutable: add /value pair Adding /value pairs is the most common way to create and change dicts. A new /value pair can be added by assigning to a not already in the dictionary: are Mutable: remove /value pair The method pop with dicts always requires an argument. Why? A /value pair can be removed by the pop method, which returns the old value in addition to removing the /value pair: 'Cazenove Hall' 'Cazenove Hall' 'sec-on' 2 'psets' 0 1 2 3 95 87 73 92 In [23]: ['psets'] = [95, 87, 73, 92] In [24]: Out[24]: {:, : 'Cazenove Hall', :, 'psets': [95, 87, 73, 92], 'section': 2, : } 15-15 'psets' In [25]:.pop('section') Out[25]: 2 0 1 2 3 95 87 73 92 In [26]: Out[26]: {:, : 'Cazenove Hall', :, 'psets': [95, 87, 73, 92], : } 15-16

are Mutable: update An existing dictionary can be updated with new /pair values through the update method. In [15]: contributions Out[15]: {'rid12': {2009: 5, 2010: 18, 2012: 4}, 'setam$3': {2012: 23, 2013: 34, 2014: 17}, 'uma52': {2015: 10, 2016: 15}} In [16]: newcontributions = {'brix4': {2011: 39, 2013: 27, 2015: 41}, 'uma52': {2015: 10, 2016: 15, 2017: 21}} In [17]: contributions.update(newcontributions) # no output In [18]: contributions Out[18]: {'brix4': {2011: 39, 2013: 27, 2015: 41}, 'rid12': {2009: 5, 2010: 18, 2012: 4}, 'setam$3': {2012: 23, 2013: 34, 2014: 17}, 'uma52': {2015: 10, 2016: 15, 2017: 21}} We can use update to change several /value pairs at one time. Summary of dictionary methods Method Name Result Mutates dict? s Returns all s as a list No values Returns all values as a list No items Returns a list of (, value) pairs No get([,val] * ) Returns corresponding value if in dict, else returns val. has_() Returns true if in dict, else false No pop() Removes +val from dict and return val. Yes update(dict1) Adds new -value pairs to the dict, replaces existing -value pairs. clear Removes all items from the dict. Yes iters, iteritems, itervalues Similar to s, values, items, but more efficient for big dictionaries, because use iterators. No Yes No * The notation [, val] means an optional second parameter. This can be omitted. 15-17 15-18 Dictionary Methods: get The get method is an alternative to using subscripting to get the value associated with a in a dictionary. It takes two arguments: (1) the (2) a default value to use if the is not in the dictionary In [22]: daysofmonth.get('oct', 'unknown') Out[22]: 31 In [23]: daysofmonth.get('oct', 'unknown') Out[23]: 'unknown' Method get is used to avoid the KeyError. It is possible to use get without a second argument (it is optional). In this case, if the doesn t exist, get will return None. To see it, we need to print the value: In [24]: print daysofmonth.get('oct') None 15-19 Dictionary Methods: s, values, items All three dict methods: s, values, items return a list with synchronized order. The s, values, and items method invocations on a dictionary return, respectively, lists of s, values, and /value pairs for a dictionary: In [25]: daysofmonth.s() Out[25]: ['Feb', 'Aug', 'Jan', 'Dec', 'Oct', 'Mar', 'Sep', 'May', 'Jun', 'Jul', 'Apr', 'Nov'] # Note the lack of order to the s! In [26]: daysofmonth.values() Out[26]: [28, 31, 31, 31, 30, 31, 30, 31, 30, 31, 30, 30] # Values have same order as s from s method invocation In [27]: daysofmonth.items() Out[27]: [('Feb', 28), ('Aug', 31), ('Jan', 31), ('Dec', 31), ('Oct', 30), ('Mar', 31), ('Sep', 30), ('May', 31), ('Jun', 30), ('Jul', 31), ('Apr', 30), ('Nov', 30)] # Items have same order as the two other methods 15-20

Iterating over a Dictionary [1] There are different ways to iterate over the /values of a dictionary. Iterating over a Dictionary [2] Avoid using the method s to iterate over the s of the dictionary. s, items, and values are useful for accessing the content of dictionaries: In [28]: for num in phones.s(): print num, phones[num] 5558671234 Gal Gadot 9996541212 Trevor Noah 7811234567 Paula A. Johnson In [29]: for number, name in phones.items(): print "Call {} at {}.".format(name, number) Call Gal Gadot at 5558671234. Call Trevor Noah at 9996541212. Call Paula A. Johnson at 7811234567. The method items returns a list of tuples, thus, we can use tuple assignment to assign to the and value at the same time (see In[29]). We re using the method format to print the formatted phrases. In [30]: for num in phones: print num, phones[num] 5558671234 Gal Gadot 9996541212 Trevor Noah 7811234567 Paula A. Johnson In [31]: for name in phones.values(): print "Call {}!".format(name) Call Gal Gadot Call Trevor Noah Call Paula A. Johnson This behaves the same as for num in phones.s() in slide 14-20. It is the recommended way to iterate over a dictionary and we will use it from now on. Don t make it a habit to use the method s with a for loop. That method is useful in some situations, but not during iteration. 15-21 15-22 An Application for dictionaries: Word Frequencies An algorithm represented as a flow chart diagram to solve a common problem. Iteration: Best Practices The methods s, items, and values always generate a list. For a big dictionary, this will occupy additional space in memory (that is, in addition to storing the dictionary, we ll have to store a separate list of s too). In [32]: type(daysofmonth.s()) Out[47]: list In such cases, it s better to use the special methods: iters, iteritems, and itervalues. They don t create an explicit list (as we can notice by using type to check for its output). We will implement the solution for this problem and others like this one in the Notebook in class. 15-23 In [33]: type(daysofmonth.iters()) Out[48]: dictionary-iterator 15-24

Membership in When looking up a in a dictionary, always use the following idiom: if somekey in somedict and never write the following: if somekey in somedict.s() A dictionary is optimized to find a instantly, that s a reason for using dictionaries. In a list, you have to search to be able to establish membership. For a long list, that will take time, especially if you do it within a LOOP, where this search will be repeated many times. 15-25 Mutability with hash When trying to use a mutable value as for a dictionary, we ll get an error: In [34]: daysofmonth[['feb', 2015]] = 28 TypeError: unhashable type: 'list' What does this error mean? It turns out, Python stores s of a dictionary as hash values, generated by the hash function. This is why dictionaries are also known as hashtables, especially in other programming languages. In [35]: hash("wellesley") Out[35]: 1371402960993349759 In [36]: hash((2015,10)) Out[36]: 3711745792089893406 In [37]: hash(1234) Out[37]: 1234 In [38]: hash([1,2,3]) TypeError: unhashable type: 'list' Only immutable objects have hash values. We ll get an error for mutable objects. 15-26 Summary 1. A dictionary is a new Python data type that is a kind of collection. It differs from lists because it stores together pairs of s and values. We use s to access values. 2. Keys are always immutable (numbers, strings, and tuples), while values can be any Python object. 3. There are at least three different ways to create a dictionary, but the most common one is to start with an empty dict and add s and values while iterating over some other data sequence. 4. are mutable, we can change the values through their s, add new /value pairs, and remove existing ones. Two examples of methods that mutate the dictionary are pop and update. 5. An important method that avoids encountering the KeyError (in case the doesn t exist) is get, which can be used with one or two arguments. 6. The methods s, values, and items return the corresponding lists, but we discourage their use when working with large dictionaries. 7. Don t write: in mydict.s(). Only write: in mydict. 15-27 Test your knowledge 1. What is the main difference between data types that are sequences and those that are collections? 2. Would you need to use range to generate indices to access the elements of a dictionary? Explain. 3. Which has to be unique: the s or the values of the dictionary? 4. When iterating over the values of a dictionary as in 15-22, is it possible to access the s too? Explain. Which of the dictionaries defined in 15-6, 8 would be a good example to make your point. 5. The diagram in 15-13 shows two boxes for assigning values to the freqdict[word]. How can you replace the if statement and those two assignments by one of the dictionary methods you learned? 6. In lecture 13 we discussed how the function sorted could sort different sequence types, always returning a list. Try out if we can use sorted with a dictionary and see what happens. 7. Challenge: Use sorted and a lambda to sort first by days and then by name for the daysofmonth dict in 15-6. The result should look like: [('Feb', 28), ('Apr', 30), ('Jun', 30), ('Nov', 30), ('Oct', 30), ('Sep', 30), ('Aug', 31), ('Dec', 31), ] 15-28