Michael Ernst CSE 140 University of Washington

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Python Data Structures CMSC 201. Built in Types Today we will be talking about some other built in types in python! Tuples Sets Dictionaries.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Dictionaries: Keeping track of pairs
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
Guide to Programming with Python
Python Control of Flow.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
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.
UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Built-in Data Structures in Python An Introduction.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
List comprehensions (and other shortcuts) UW CSE 160 Spring 2015.
Collections Michael Ernst CSE 190p University of Washington.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Sets Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
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
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Dictionaries Ruth Anderson UW CSE 160 Winter
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Sharing, mutability, and immutability
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2017.
List comprehensions (and other shortcuts) UW CSE 160 Winter 2016.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
CSC 108H: Introduction to Computer Programming
List comprehensions (and other shortcuts) UW CSE 160 Winter 2017.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2017.
CSC 108H: Introduction to Computer Programming
Ruth Anderson CSE 140 University of Washington
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Sharing, mutability, and immutability
Ruth Anderson UW CSE 160 Spring 2018
Ruth Anderson UW CSE 160 Winter 2017
Introduction to Python and programming
CHAPTER THREE Sequences.
Sharing, mutability, and immutability
CS190/295 Programming in Python for Life Sciences: Lecture 6
Sharing, mutability, and immutability
Intro to Computer Science CS1510 Dr. Sarah Diesburg
List comprehensions (and other shortcuts) UW CSE 160 Spring 2018.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2018.
Iteration Abstraction
Sharing, mutability, and immutability
Presented by Mirza Elahi 10/29/2018
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2018.
Ruth Anderson UW CSE 160 Winter 2017
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Introduction to Python and programming
Python Primer 1: Types and Operators
Sharing, mutability, and immutability
Michael Ernst CSE 140 University of Washington
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2016.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CHAPTER 4: Lists, Tuples and Dictionaries
Ruth Anderson CSE 160 University of Washington
And now for something completely different . . .
Ruth Anderson UW CSE 160 Spring 2018
CSE 231 Lab 7.
CSE 231 Lab 9.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2019.
Introduction to Python and programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Dictionary.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

Michael Ernst CSE 140 University of Washington Sets Michael Ernst CSE 140 University of Washington

Sets Mathematical set: a collection of values, without duplicates or order Order does not matter { 1, 2, 3 } == { 3, 2, 1 } No duplicates { 3, 1, 4, 1, 5 } == { 5, 4, 3, 1 } For every data structure, ask: How to create How to query (look up) and perform other operations (Can result in a new set, or in some other datatype) How to modify Answer: http://docs.python.org/2/library/stdtypes.html#set 2 3 1 4 5 1 Should ask how many people are already familiar with the notion of a set. I suspect many students are not! 3

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

Set operations odd = { 1, 3, 5 } prime = { 2, 3, 5 } membership  Python: in 4 in prime  False union  Python: | odd | prime  { 1, 2, 3, 5 } intersection  Python: & odd & prime  { 3, 5 } difference \ or - Python: - odd – prime  { 1 } Iteration over sets: # iterates over items in arbitrary order for item in myset: … Think in terms of set operations, not in terms of iteration and element operations Shorter, clearer, less error-prone, faster

Modifying a set Add one element to a set: myset.add(newelt) myset = myset | { newelt } Remove one element from a set: myset.remove(elt) # elt must be in myset myset.discard(elt) # never errs myset = myset - { elt } What would this do? myset = myset - elt Choose and remove some element from a set: myset.pop()

Practice with sets

List vs. set operations (1) Find the common elements in both list1 and list2: out1 = [] for i in list2: if i in list1: out1 .append(i) # We will learn about list comprehensions later out1 = [i for i in list2 if i in list1] Find the common elements in both set1 and set2: set1 & set2 Much shorter, clearer, easier to write!

List vs. set operations (2) Find the elements in either list1 or list2 (or both): out2 = list(list1) # make a copy for i in list2: if i not in list1: out2.append(i) out2 = list1+list2 for i in out1: # out1 = common elements in both lists out2.remove(i) Find the elements in either set1 or set2 (or both): set1 | set2

List vs. set operations (3) Find the elements in either list but not in both: out3 = [] for i in list1+list2: if i not in list1 or i not in list2: out3.append(i) Find the elements in either set but not in both: set1 ^ set2

Not every value may be placed in a set Set elements must be immutable values int, float, bool, string, tuple not: list, set, dictionary Goal: only set operations change the set 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 }  Hypothetical; actually illegal in Python list1 in myset  True list3 in myset  True list2.append("c") list1 in myset  ??? list3 in myset  ???