Collections. Michael Ernst CSE 190p University of Washington

Similar documents
Sorting. Ruth Anderson UW CSE 160 Winter 2017

Sorting. Sorting. Customizing the Sort Order. Sorting

Sorting. BBM Introduction to Programming I. Hacettepe University Fall Fuat Akal, Aykut Erdem, Erkut Erdem

Sorting. Ruth Anderson UW CSE 160 Spring 2018

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

CSE 140 Winter 2014: Midterm Exam

Lists. Ruth Anderson University of Washington CSE 160 Spring 2015

BBM 101 Introduction to Programming I Lecture #07 Sorting, List Comprehension, Data Visualization

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

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

Lists. Ruth Anderson University of Washington CSE 160 Winter 2017

Introduction to Python

Overview of List Syntax

CS1 Lecture 3 Jan. 18, 2019

CSE 140 wrapup. Michael Ernst CSE 140 University of Washington

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

CS1 Lecture 3 Jan. 22, 2018

Selection Statement ( if )

COMP1730/COMP6730 Programming for Scientists. Dictionaries and sets

COMP 204: Dictionaries Recap & Sets

MEMOIZATION, RECURSIVE DATA, AND SETS

More Data Structures. What is a Dictionary? Dictionaries. Python Dictionary. Key Value Pairs 10/21/2010

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

python 01 September 16, 2016

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

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

CS 230 Programming Languages

Course Outline - COMP150. Lectures and Labs

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

COMP 204: Sets, Commenting & Exceptions

Programming Languages and Techniques (CIS120)

Lists How lists are like strings

CSC108: Introduction to Computer Programming. Lecture 11

CS 1110 Prelim 2 November 13th, 2014

TUPLES AND RECURSIVE LISTS 5

Beyond Blocks: Python Session #1

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

COMP10001 Foundations of Computing Functions

Lecture 7: Lambda & Abstract Data Types

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

Introduction to Python

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

More Loop Examples Functions and Parameters

ECS15, Lecture 14. Reminder: how to learn Python. Today is the final day to request a regrade of the midterm. Topic 4: Programming in Python, cont.

Advanced Algorithms and Computational Models (module A)

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

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

Collections. Lists, Tuples, Sets, Dictionaries

Programming Languages

List comprehensions (and other shortcuts) UW CSE 160 Spring 2015

Python a modern scripting PL. Python

Announcements for this Lecture

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

9/19/12. Why Study Discrete Math? What is discrete? Sets (Rosen, Chapter 2) can be described by discrete math TOPICS

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

The Dynamic Typing Interlude

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

Data Structures. Lists, Tuples, Sets, Dictionaries

Python. Karin Lagesen.

MUTABLE LISTS AND DICTIONARIES 4

a b c d a b c d e 5 e 7

LECTURE 3 Python Basics Part 2

CMSC201 Computer Science I for Majors

VALLIAMMAI ENGINEERING COLLEGE

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

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

Principles of Computer Science

CS1 Lecture 12 Feb. 11, 2019

Intro. Scheme Basics. scm> 5 5. scm>

Programming Languages

Lecture 7. Memory in Python

Eric Roberts Handout #39 CS 106B February 20, 2015 Sets

Large-Scale Networks

CIS192: Python Programming

Data Handing in Python

Problem Solving for Intro to Computer Science

Introduction to Python! Lecture 2

Python Evaluation Rules

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

A. Python Crash Course

The Pyth Language. Administrivia

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

DSC 201: Data Analysis & Visualization

Lecture #21: Search and Sets. Last modified: Wed Mar 9 15:44: CS61A: Lecture #21 1

CSC Discrete Math I, Spring Sets

Script language: Python Data structures

Lecture 21. Chapter 12 More Python Containers

Python for Non-programmers

Types, lists & functions

CSC Software I: Utilities and Internals. Programming in Python

COMP1730/COMP6730 Programming for Scientists. Sequence types, part 2

CMSC201 Computer Science I for Majors

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

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

More Counting Sort and Sorting-by-Key

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

CSE : Python Programming

CSE : Python Programming. Decorators. Announcements. The decorator pattern. The decorator pattern. The decorator pattern

Lecture #10: Sequences. Last modified: Mon Feb 22 16:33: CS61A: Lecture #10 1

Lecture Notes on Binary Decision Diagrams

Transcription:

Collections Michael Ernst CSE 190p University of Washington

Needed for Homework 4 (social networking assignment) Collections: lists, sets, dictionaries Sorting Graphs

Outline for today Collections (built-in data structures) Lists Sets Sorting Order independence Dictionaries (mappings) Graphs

How to evaluate list expressions There are two new forms of expression: [a, b, c, d] list creation List expression a[b] Index expression To evaluate: evaluate each element to a value, from left to right make a list of the values The elements can be arbitrary values, including lists ["a", 3, 3.14*r*r, fahr_to_cent(-40), [3+4, 5*6]] To evaluate: list indexing or dereferencing evaluate the list expression to a value evaluate the index part to a value if the list value is not a list, execution terminates with an error if the element is not in range (not a valid index), execution terminates with an error the value is the given element of the list value

List slicing mylist[startindex : endindex]evaluates to a sublistof the original list mylist[index]evaluates to an elementof the original list Arguments are like those to the rangefunction start index is inclusive end index is exclusive optional step argument: mylist[st : end : step] See handout for practice problems

Sorting hamlet = "to be or not to be that is the question whether tis nobler in the mind to suffer".split() print "hamlet:", hamlet print "sorted(hamlet):", sorted(hamlet) print "hamlet:", hamlet print "hamlet.sort():", hamlet.sort() print "hamlet:", hamlet Lists are mutable they can be changed including by functions

Customizing the sort order Goal: sort a list of names by last name names = ["Isaac Newton", "Albert Einstein", "Niels Bohr", "Charles Darwin", "Louis Pasteur", "Sigmund Freud", "Galileo Galilei"] print "names:", names This does not work: print "sorted(names):", sorted(names) When sorting, how should we compare these names? "Niels Bohr" "Charles Darwin" A sort key is a different value that you use to sort a list, instead of the actual values in the list def last_name(str): return str.split(" ")[1] print 'last_name("isaac Newton"):', last_name("isaac Newton")

Two ways to use a sort key 1. Create a different list that contains the sort key, sort it, then extract the part you care about keyed_names = [[last_name(name), name] for name in names] print "keyed_names:", keyed_names print "sorted(keyed_names):", sorted(keyed_names) print "sorted(keyed_names, reverse = True):" print sorted(keyed_names, reverse = True) (This works because Python compares two elements that are lists elementwise.) sorted_names = [keyed_name[1] for keyed_name in sorted(keyed_names, reverse = True)] print "sorted_names:", sorted_names 2. Supply the keyargument to the sortedfunction or the sortfunction print "sorted(names, key = last_name):" print sorted(names, key = last_name) print "sorted(names, key = last_name, reverse = True):" print sorted(names, key = last_name, reverse = True)

Sets Mathematical set: a collection of values, without duplicates or order Two ways to create a set: Direct mathematical syntax odd = { 1, 3, 5 } prime = { 2, 3, 5 } Cannot express empty set: {} means something else Construct from a list odd = set([1, 3, 5]) prime = set([2, 3, 5]) empty = set([]) Python always prints using this syntax Order does not matter { 1, 2, 3 } == { 3, 2, 1 } No duplicates set([3,1,4,1,5]) == { 5, 4, 3, 1 }

Set operations odd = { 1, 3, 5 } prime = { 2, 3, 5 } union Python: odd prime { 1, 2, 3, 5 } intersection Python: & odd & prime { 3, 5 } difference \or - Python: - odd prime { 1 } membership Python: in 4 in prime False Iteration over sets: # iterates over items in arbitrary order for item in myset: Add one element to a set: myset.add(newelt) myset = myset {newelt} Think in terms of set operations, not in terms of iteration and element operations Shorter, clearer, less error-prone, faster

Practice with sets

Not every value may be placed in a set Set elements must be immutable values int, float, bool, string, tuple not: list, set, dictionary Goals after myset.add(x), x in myset True y in myset always evaluates to the same value Both conditions should hold until myset is changed Mutable elements can violate these goals list1 = ["a", "b"] list2 = list1 list3 = ["a", "b"] myset = {list1} list1 in myset True list3 in myset True list2.append("c") list1 in myset??? list3 in myset??? Hypothetical; actually illegal in Python

Computing a histogram Recall exercise from previous lecture: For each word in a text, record the number of times that word appears Withoutthinking about any Python data structures, how would you solve this? Always start by thinking about the data, notby thinking about how you would implement it hamlet = "to be or not to be that is the question whether tis nobler in the mind to suffer".split()

Dictionaries or mappings A dictionary maps each keyto a value d = { } us_wars1 = { "Revolutionary" : [1775, 1783], "Mexican" : [1846, 1848], "Civil" : [1861, 1865] } us_wars2 = { 1783: "Revolutionary", 1848: "Mexican", 1865: "Civil" } Syntax just like arrays, for accessing and setting: us_wars2[1783][1:10] evolution us_wars1["wwi"] = [1917, 1918] Order does not matter { 5 : 25, 6 : 36, 7 : 49 } == { 7 : 49, 5: 25, 6 : 36 }

Not every value is allowed to be a key Keys must be immutable values int, float, bool, string, tuple not: list, set, dictionary Goals 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" mydict[list3] "z" list2.append("c") mydict[list1]??? mydict[list3]??? Hypothetical; actually illegal in Python

Graphs A graph can be thought of as: a collection of edges for each node, a collection of neighbors