Ruth Anderson UW CSE 160 Winter 2016

Slides:



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

Dictionaries: Keeping track of pairs
Guide to Programming with Python
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
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.
CIT 590 Intro to Programming Lecture 5 – completing lists.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
1 Joe Meehean.  List of names  Set of names  Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: Bill:
List comprehensions (and other shortcuts) UW CSE 160 Spring 2015.
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.
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.
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
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
Sharing, mutability, and immutability
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
Introduction to Python and programming
List comprehensions (and other shortcuts) UW CSE 160 Winter 2016.
List comprehensions (and other shortcuts) UW CSE 160 Winter 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
Introduction to Python and programming
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
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
List comprehensions (and other shortcuts) UW CSE 160 Spring 2018.
ARRAYS 1 GCSE COMPUTER SCIENCE.
Sharing, mutability, and immutability
String and Lists Dr. José M. Reyes Álamo.
Presented by Mirza Elahi 10/29/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
Michael Ernst CSE 140 University of Washington
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Dictionaries: Keeping track of pairs
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson CSE 160 University of Washington
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
And now for something completely different . . .
Ruth Anderson UW CSE 160 Spring 2018
CSE 231 Lab 9.
Introduction to Python and programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Sample lecture slides.
Dictionary.
Tuple.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

Ruth Anderson UW CSE 160 Winter 2016 Sets Ruth Anderson UW CSE 160 Winter 2016

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 3 Should ask how many people are already familiar with the notion of a set. I suspect many students are not!

Two ways to create a set Direct mathematical syntax: odd = { 1, 3, 5 } prime = { 2, 3, 5 } Note: Cannot use “{}” to express empty set: it means something else . Construct from a list: (also from a tuple or string) odd = set( [1, 3, 5] ) prime = set( [2, 3, 5] ) empty = set( [] ) # or set( ) Python always prints using this syntax above

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 } Think in terms of set operations, not in terms of iteration and element operations Shorter, clearer, less error-prone, faster Although we can do iteration over sets: # iterates over items in arbitrary order for item in myset: … But we cannot index into a set to access a specific element.

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 or raises err myset.discard(elt) # never errs myset = myset - { elt } What would this do? myset = myset – elt Remove and return an arbitrary element from a set: myset.pop()

Practice with sets z = {5,6,7,8} y = {1,2,3,"foo",1,5} k = z & y j = z | y m = y – z n = z – y p = z q = set(z) z.add(9)

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 with sets!

List vs. set operations(2) Find elements in either list1 or list2 (or both) (without duplicates): out2 = list(list1) # make a copy for i in list2: if i not in list1: # don’t append elements already in out2 out2.append(i) Another way: out2 = list1+list2 for i in out1: # out1 = common elements in both lists out2.remove(i) # Remove common elements ----------------------------------------------------------------------- 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 The set itself is mutable (e.g. we can add and remove elements) 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 Aside: frozenset must contain immutable values and is itself immutable (cannot add and remove elements)