Adapted from slides by Marty Stepp and Stuart Reges

Slides:



Advertisements
Similar presentations
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
Advertisements

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
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
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
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 17: Arrays for Tallying; Text Processing reading: 4.3, 7.6 (Slides adapted.
CSE 143 Lecture 11: Sets and Maps reading:
CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations.
CSc 110, Autumn 2016 Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 16: Fencepost Loops and Review
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries
When to use Tuples instead of Lists
Adapted from slides by Marty Stepp and Stuart Reges
Containers and Lists CIS 40 – Introduction to Programming in Python
CSc 110, Autumn 2016 Lecture 27: Sets and Dictionaries
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2017 Lecture 14: Boolean Logic and Midterm Review
CSc 110, Autumn 2017 Lecture 24: Lists for Tallying; Text Processing
Adapted from slides by Marty Stepp and Stuart Reges
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
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 33: Dictionaries
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
CSc 110, Spring 2018 Lecture 17: while Loops and decomposition
Lecture 23: Tuples Adapted from slides by Marty Stepp and Stuart Reges
Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges
Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 9: Graphics and Nested Loops
CSc 110, Autumn 2017 Lecture 27: Lists that change size and Tuples
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 23: Tuples Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 7: input and Constants
slides adapted from Marty Stepp and Hélène Martin
Lecture 19: lists Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2017 Lecture 20: Lists for Tallying; Text Processing
Dictionaries Dictionary: object that stores a collection of data
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
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
slides created by Marty Stepp
Building Java Programs
CSc 110, Spring 2018 Lecture 8: input and Parameters
CSc 110, Spring 2018 Lecture 10: More Graphics
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 8: More Graphics
slides created by Marty Stepp
Why are arrays useful? We can use arrays to store a large amount of data without declaring many variables. Example: Read in a file of 1000 numbers, then.
slides adapted from Marty Stepp and Hélène Martin
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
slides adapted from Marty Stepp
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
Adapted from slides by Marty Stepp and Stuart Reges
Dictionary.
Presentation transcript:

Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Spring 2018 Lecture 34: 2D Structures Adapted from slides by Marty Stepp and Stuart Reges

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"

Dictionaries and tallying a dictionary can be thought of as generalization of a tallying list the "index" (key) doesn't have to be an int count digits: 22092310907 # (Roosevelt), (L)andon, (I)ndependent count votes: "RLLLLLLRRRRRLLLLLLRLRRIRLRRIRLLRIR" index 1 2 3 4 5 6 7 8 9 value "R" "L" "I" 15 3 keys values key "R" "L" "I" value 15 3

Dictionary operations items() 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.

Looping over a set or dictionary? You must use a for element in structure loop needed because sets have no indexes; can't get element i Example: for item in a: print(item) Outputs: the happy hello

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["Purrcival"] = 12 for cat, age in ages.items(): print(cat + " -> " + 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

Exercise Use word counts to figure out if a document is positive or negative Count all of the positive words and count all of the negative words. Whichever count is bigger is the sentiment of the document. How do we know which words are positive and which are negative?

Exercise Consider the following function: def mystery(list1, list2): result = {} for i in range(0, len(list1)): result[list1[i]] = list2[i] result[list2[i]] = list1[i] return result What is returned after calls with the following parameters? list1: [b, l, u, e] list2: [s, p, o, t] dictionary returned:__________________________________________________________ list1: [k, e, e, p] list2: [s, a, f, e] list1: [s, o, b, e, r] list2: [b, o, o, k, s]

What is the right structure? You want to store a bunch of colors so you can later choose one at random. Batting order of a baseball team. Students names and their grades on a project. Friends names and their phone numbers Height, width and location of a sports field. Movies a person has watched. Items in a shopping cart. A student's grades.

What is the right structure? The grades for all students in a class All books in a store arranged by category Many recipes each containing many steps Phone numbers that have been called this month on a phone plan divided by area and country code for billing simplicity

Exercise We would like to store data for the class so that we can: Access the entire class list easily Access a section list easily What structure is appropriate for this problem? Sometimes it can be helpful to store a structure inside another structure