CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
CSE 143 Lecture 15 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
Built-in Data Structures in Python An Introduction.
CSE 143 Lecture 14 (B) Maps and Grammars reading: 11.3 slides created by Marty Stepp
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
CSE 143 Lecture 14 Maps/Sets; Grammars reading: slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 11: Sets and Maps reading:
CSc 110, Autumn 2016 Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges.
Building Java Programs
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CSCI 104 Abstract Data Types
CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries
Adapted from slides by Marty Stepp and Stuart Reges
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
slides created by Marty Stepp and Hélène Martin
CSc 110, Autumn 2016 Lecture 27: Sets and Dictionaries
C++ Standard Library.
CSc 110, Spring 2017 Lecture 28: Sets and Dictionaries
Lecture 3: Working with Data Structures
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Road Map CS Concepts Data Structures Java Language Java Collections
TCSS 143, Autumn 2004 Lecture Notes
Data Structures and Algorithms
TCSS 342, Winter 2006 Lecture Notes
Sets (11.2) set: A collection of unique values (no duplicates allowed) that can perform the following operations efficiently: add, remove, search (contains)
suggested reading: Java Ch. 13.2
CSc 110, Spring 2018 Lecture 33: Dictionaries
CSE 373: Data Structures and Algorithms
Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 27: Lists that change size and Tuples
Adapted from slides by Marty Stepp and Stuart Reges
EE 422C Sets.
CSE 373: Data Structures and Algorithms
ARRAYS 1 GCSE COMPUTER SCIENCE.
Adapted from slides by Marty Stepp and Stuart Reges
CSE 373: Data Structures and Algorithms
Ruth Anderson UW CSE 160 Winter 2017
Adapted from slides by Marty Stepp and Stuart Reges
Exercise Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a collection.
CS2110: Software Development Methods
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CS 1111 Introduction to Programming Fall 2018
Dictionaries Dictionary: object that stores a collection of data
CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges.
CSE 373 Java Collection Framework, Part 2: Priority Queue, Map
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
slides created by Marty Stepp
Building Java Programs
Sets (11.2) set: A collection of unique values (no duplicates allowed) that can perform the following operations efficiently: add, remove, search (contains)
Adapted from slides by Marty Stepp and Stuart Reges
CISC101 Reminders Assignment 2 due today.
Lecture 3: Maps and Iterators
slides created by Marty Stepp
CSE 143 Lecture 15 Sets and Maps; Iterators reading: ; 13.2
Ruth Anderson UW CSE 160 Spring 2018
slides adapted from Marty Stepp and Hélène Martin
Web Design & Development Lecture 6
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CSE 373 Set implementation; intro to hashing
Why not just use Arrays? Java ArrayLists.
Adapted from slides by Marty Stepp and Stuart Reges
Dictionary.
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries Adapted from slides by Marty Stepp and Stuart Reges

Exercise Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a structure and report the # of unique words. Once you've created this structure, allow the user to search it to see whether various words appear in the text file. What structure is appropriate for this problem? List? Tuple?

Sets set: A collection of unique values (no duplicates allowed) that can perform the following operations efficiently: add, remove, search (contains) We don't think of a set as having indexes; we just add things to the set in general and don't worry about order "to" in set true set "the" "of" "from" "to" "she" "you" "him" "why" "in" "down" "by" "if" "be" in set false

Creating a Set An empty set: A set with elements in it: a = set() b = {"the", "hello", "happy"} a.add(val) adds element val to a a.discard(val) removes val from a if present a.pop() removes and returns a random element from a a - b returns a new set containing values in a but not in b a | b returns a new set containing values in either a or b a & b returns a new set containing values in both a and b a ^ b returns a new set containing values in a or b but not both You can also use in, len(), etc.

Looping over a set? You must use a for element in structure loop needed because sets have no indexes; can't get element i

Exercise Write a program to count the number of occurrences of each unique word in a large text file (e.g. Moby Dick ). Allow the user to type a word and report how many times that word appeared in the book. Report all words that appeared in the book at least 500 times. What structure is appropriate for this problem?

Dictionaries dictionary: Holds a set of unique keys and a collection of values, where each key is associated with one value. a.k.a. "map", "associative array", "hash" basic dictionary operations: put(key, value ): Adds a mapping from a key to a value. get(key ): Retrieves the value mapped to the key. remove(key ): Removes the given key and its mapped value. my_dict["Juliet"] returns "Capulet"

Dictionary functions You can also use in, len(), etc. my_dict[key] = value) adds a mapping from the given key to the given value; if the key already exists, replaces its value with the given one my_dict[key] returns the value mapped to the given key (error if key not found) items(key) return a new view of the dictionary’s items ((key, value) pairs) pop(key) removes any existing mapping for the given key and returns it (error if key not found) popitem() removes and returns an arbitrary (key, value) pair (error if empty) keys() returns the dictionary's keys values() returns the dictionary's values You can also use in, len(), etc.

Using dictionaries A dictionary allows you to get from one half of a pair to the other. Remembers one piece of information about every index (key). Later, we can supply only the key and get back the related value: Allows us to ask: What is Suzy's phone number? # key value my_dict["Suzy"] = "206-685-2181" Dictionary my_dict["Suzy"] Dictionary "206-685-2181"

Maps and tallying a map can be thought of as generalization of a tallying list the "index" (key) doesn't have to be an int count digits: 22092310907 # (T)rump, (C)linton, (I)ndependent count votes: "TCCCCCCTTTTTCCCCCCTCTTITCTTITCCTIC" index 1 2 3 4 5 6 7 8 9 value "T" "C" "I" 15 3 keys values key "T" "C" "I" value 15 3

items, keys and values items function returns tuples of each key-value pair can loop over the keys in a for loop ages = {} ages["Merlin"] = 4 ages("Chester"] = 2 ages["Percival"] = 12 for cat, age in ages.items()): print(name + " -> " + str(age)) values function returns all values in the dictionary no easy way to get from a value to its associated key(s) keys function returns all keys in the dictionary