CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries

Slides:



Advertisements
Similar presentations
1 VBScript Session What we learn last session? Regulars Expressions. Methods and properties. How to use the object and his collections. How to create.
Advertisements

Why not just use Arrays? Java ArrayLists.
CSC1016 Coursework Clarification Derek Mortimer March 2010.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
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
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
© 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
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
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:
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.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
CSE 143 Lecture 14 Maps/Sets; Grammars reading: slides adapted from Marty Stepp and Hélène Martin
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
CSE 143 Lecture 11: Sets and Maps reading:
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
Building Java Programs
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CSc 110, Spring 2017 Lecture 18: Line-Based File Input
Adapted from slides by Marty Stepp and Stuart Reges
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
CSc 120 Introduction to Computer Programing II
slides created by Marty Stepp and Hélène Martin
CSc 110, Autumn 2016 Lecture 27: Sets and Dictionaries
Adapted from slides by Marty Stepp and Stuart Reges
C++ Standard Library.
Announcements Project 4 due Wed., Nov 7
Data types: Complex types (Dictionaries)
CSc 110, Spring 2017 Lecture 28: Sets and Dictionaries
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
Sets (11.2) set: A collection of unique values (no duplicates allowed) that can perform the following operations efficiently: add, remove, search (contains)
Computer Science 2 Hashing
Chapter 7 Files and Exceptions
CSc 110, Spring 2018 Lecture 33: Dictionaries
CSE 373: Data Structures and Algorithms
CHAPTER THREE Sequences.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 14: Booleans and Strings
Adapted from slides by Marty Stepp and Stuart Reges
CEV208 Computer Programming
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.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
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
Sets (11.2) set: A collection of unique values (no duplicates allowed) that can perform the following operations efficiently: add, remove, search (contains)
slides created by Marty Stepp
Introduction to Computer Science
Adapted from slides by Marty Stepp and Stuart Reges
slides adapted from Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp and Stuart Reges
Dictionary.
Introduction to PYTHON
Presentation transcript:

CSc 110, Spring 2017 Lecture 29: 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?

Unique Words # outputs the number unique words in a file def main(): all_words = file_to_words("mobydick.txt") print("unique word count " + str(len(all_words))) # creates and returns a set containing all of the words from the # file with the passed in name stripped of punctuation. def file_to_words(file_name): file = open(file_name) words = file.read() # get rid of punctuation words = words.replace(",", "") words = words.replace(".", "") words = words.replace("!", "") words = words.split() s = set() for word in words: s.add(word.lower()) return s main()

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 collection of zero or more key/value pairs a.k.a. "map", "associative array", "hash" basic dictionary operations: Add a mapping from a key to a value. Retrieve a value mapped to a key. Remove a given key and its mapped value.

Creating dictionaries Creating a dictionary {key : value, …, keyn : valuen} names = {"Romeo" : "Montague", "Tyler" : "Durden", "Tybalt" : "Capulet", "Juliet" : "Capulet" } dictionary[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 Accessing values: dictionary[key] retuns the value mapped to the given key (error if key not found) names["Juliet"] produces "Capulet"

Using dictionaries A dictionary allows you to get from one half of a pair to the other. Associates one piece of information for every key. Using the key as an index produces the related value: Allows us to ask: What is Suzy's phone number? # key value phones["Suzy"] = "206-685-2181" Dictionary phones["Suzy"] Dictionary "206-685-2181"

Lists must be indexed by integers count digits: 22092310907 Dictionaries can be indexed by integers, strings, tuples and more # (R)oosevelt, (L)andon, (I)ndependent count votes: "RLLLLLLRRRRRLLLLLLRLRRIRLRRIRLLRIRR" index 1 2 3 4 5 6 7 8 9 value "R" "L" "I" 16 3 15 keys values key "R" "L" "I" value 16 15 3

Checking for a key with the in operator The in operator returns True if the dictionary contains the specified key and False otherwise. >>> ages = {} >>> ages["Joe"] = 10 >>> ages {'Joe': 10} >>> "Joe" in ages True >>> "Tom" in ages False

Looping through dictionaries The for loop can be used to loop through the keys in a dictionary ages = {} ages["Merlin"] = 4 ages["Chester"] = 2 ages["Percival"] = 12 for name in ages: print(name, ages[name]) Output: Merlin 4 Chester 2 Percival 12

Example Write a function count_chars that takes a string and returns a dictionary of the counts of all characters in the string. Using a dictionary: The keys will be the characters The values will be the counts.

Example Write a function count_chars that takes a string and returns a dictionary of the counts of all characters in the string. Using the name counts for the dictionary, to update the count, we use the following: counts[c] = counts[c] + 1 What happens the first time we encounter a new character c? Must check first to see if it's there.

Dictionary methods You can also use len(), etc. items() returns a sequence of tuples (key, value) representing the 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 len(), etc.

items, keys and values The items method can be used to loop through all the key/value pairs in a dictionary ages = {} ages["Merlin"] = 4 ages["Chester"] = 2 ages["Percival"] = 12 for tup in ages.items(): print(tup[0] + " -> " + str(tup[1])) 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