Compsci 101.2, Fall 2015 18.1 Plan for October 29 l Review dictionaries and their use  Very efficient, easy to use  Efficiency doesn't matter much for.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Dictionaries: Keeping track of pairs
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises.
Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.
Title An Introduction to Python. What is Python exactly? Python is a modern rapid development language. Code is very clean and easy to read. Emphasizes.
COMPSCI 105 S Principles of Computer Science Linked Lists 2.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
Tutorial 10 Dictionaries and Classes. Reminder Assignment 09 is due April 6 at 11:55pm (last day of classes) Next tutorial is on April 6 (Monday)
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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.
General Programming Introduction to Computing Science and Programming I.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Compsci 06/101, Spring Compsci 6/101: PFTW, Feb 28-March 4 l Algorithms and Data Structures  Sets and how they are used in Python (data structure)
Compsci 101.2, Fall Plan for FFWOO l Programming in the small and in the large  Making one function work correctly, especially in the context.
Compsci 101.2, Fall Plan for TDAFB (after fall break) l Hear via about plans to catch-up and stay-ahead  Profs. Rodger and Astrachan to.
Compsci 101.2, Fall Plan for FWON l Review current assignments and APTs  Review Dictionaries and how to use them  Code and APT walk-through.
Compsci 101.2, Fall Plan for 11/9 and 11/11 l Reviewing Concepts for test  Loops: index and by element  Strings, Lists, Sets, Dictionaries.
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.
Compsci 101.2, Fall Plan for WBTB l APT Quiz 3 – due tonight l Solving problems in the wild  How can you change how things are sorted Other.
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
Compsci 101.2, Fall Plan for October 22 l Images, tuples, RGB color model  Image processing by understanding API  Image processing with tuples.
Compsci 6/101, Spring WBFSB l Plan for this week  Get test back, review, look for problems/weaknesses  How to address these?  Assignments.
Compsci 101.2, Fall Plan for eleven-four l Thinking about APTs and test problems  How do you choose: list, string, set, dictionary  Experience?
Maps Nick Mouriski.
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
Tutorial 10 Dictionaries and Classes. Reminder Assignment 09 is due FRIDAY, Dec 4 at 4:00 pm.
Compsci 101.2, Fall Plan for October l Structuring Data and Code for Efficiency  Computer time, how expensive is it?  Data storage, how.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction to Python Aug 22, 2013 Hee-gook Jun.
Compsci 06/101, Spring This week in Compsci 6/101 l From sets to dictionaries  Incredibly powerful in solving problems  Power in Perl, Python,
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
CompSci 101 Introduction to Computer Science March 24, 2016 Prof. Rodger compsci 101, spring
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
Section06: Sequences Chapter 4 and 5. General Description "Normal" variables x = 19 – The name "x" is associated with a single value Sequence variables:
CompSci 101 Introduction to Computer Science March 29, 2016 Prof. Rodger.
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
Algorithmic complexity: Speed of algorithms
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
CMSC201 Computer Science I for Majors Lecture 22 – Searching
Containers and Lists CIS 40 – Introduction to Programming in Python
CompSci 101 Introduction to Computer Science
Bryan Burlingame Halloween 2018
CHAPTER THREE Sequences.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CompSci 101 Introduction to Computer Science
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Ruth Anderson UW CSE 160 Winter 2017
Algorithmic complexity: Speed of algorithms
Nate Brunelle Today: Dictionaries
Dictionaries: Keeping track of pairs
Algorithmic complexity: Speed of algorithms
Bryan Burlingame Halloween 2018
Ruth Anderson UW CSE 160 Spring 2018
Nate Brunelle Today: Dictionaries
James Wei Professor Peck 9/20/2013
Dictionary.
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

Compsci 101.2, Fall Plan for October 29 l Review dictionaries and their use  Very efficient, easy to use  Efficiency doesn't matter much for small data  Programmer time, how expensive is it? l Review APTs, reminder about APT quiz  Quiz must be done alone, we don't look at code But we could look at code to ensure no copying!  Quiz will be mostly straightforward application If you're up-to-speed on APTs this week, good!

Compsci 101.2, Fall A Python view of dictionaries l A collection of (key,value) pairs that is similar syntactically to a list  A list can be accessed by index: a[3]  A dictionary can be accessed by key: d["cat"] l The key in a dictionary must be immutable  Essentially because key converted to number and number used as index (to find value) l Finding the value associated with a key is very fast  Essentially doesn't depend on # keys!

Compsci 101.2, Fall Python syntax for dictionaries l Coding with Dictionaries  Error to access d[key] for update if key not in d Dictionary Syntax/FunctionMeaning d.items() List of (key,value) tuples d.keys() List of keys d.values() List of values d.get(key) Like d[key], no error d Query like d.keys()

Compsci 101.2, Fall Case Study: Counting # occurrences See Counter.py, what does function countup return? Conceptually?  Words is a list of strings  Sorting tuples looks at first element, breaks ties with second ("dog",2) > ("cat",4) ['dog','cat','bug','cat','dog','cat','cat','bug'] def countup(words): pairs = [(w,words.count(w)) for w in words] return sorted(set(pairs))

Compsci 101.2, Fall Counting more quickly What makes countup "slow"?  Why is a set returned? Why a sorted set?  How many times is words.count(w) called?  Making countup faster vs. a new approach Let's use a dictionary! def countup(words): pairs = [(w,words.count(w)) for w in words] return sorted(set(pairs))

Compsci 101.2, Fall Counting more quickly l Easy to code, use words.count! But after counting 'dog', we count 'cat', and then …  Look at a million words in counting a thousand  Instead, look at words once! Update per-word counter, so much faster with dictionaries! def fastcount(words): d = {} for w in words: if w in d: d[w] += 1 else: d[w] = 1 return sorted(d.items())

Compsci 101.2, Fall Answer Questions

Compsci 101.2, Fall danah boyd Dr. danah boyd is a Senior Researcher at Microsoft Research, … a Visiting Researcher at Harvard Law School, …Her work examines everyday practices involving social media, with specific attention to youth engagement, privacy, and risky behaviors. She heads Data & Society (Research Institute) and recently authored It's Complicated: The Social Lives of Networked Teens. " we need those who are thinking about social justice to understand technology and those who understand technology to commit to social justice."

Compsci 101.2, Fall Solving APTs l If Harry pays Sally $10.23,  "Harry:Sally:10.23" and Harry is out $10.23 l Given a string in this form, how do we extract payer, payee, amount?  Conceptually  In Python

Compsci 101.2, Fall After extracting transaction info … l Why is a dictionary useful? What are (key,value) pairs?  Think about how to do this by hand, keep a sheet with each person's name, update the amount next to their name  Look up name, get amount l General dictionary update methods  Check if key seen before, update d[key] +=  If not seen, initialize first time, d[key] =

Compsci 101.2, Fall So many APTs have this format! l Initialize structure before looping over data  List, set, string, dictionary l Loop over data and update structure  Extract info from element, update by.add,.append, +=, etc. l May need to process structure for return  Sort, remove some, change format, etc.  What does d.items() return for a dictionary? List of (key,value) tuples!

Compsci 101.2, Fall Finishing up VenmoTracker l Value stored in dictionary before return [('drew', 10.0), ('owen', -30.0), ('robert', 10.0), ('susan', 10.0)] l How do access name and amount in each tuple? How do we loop over tuples? l How do we create a string from a string and a float? l How do we sort, when do we sort?

Compsci 101.2, Fall Answer Questions

Compsci 101.2, Fall Member Club APT l Given two lists A and B, how can you find a list of values in both lists?  for x in A: if x in B:  both = list(set(A)& set(B)) Ideally you'll see the set solution quickly, but solving the problem is important!

Compsci 101.2, Fall DictionaryTimings.py l Updating (key,value) pairs in structures  Search through unordered list  Search through ordered list  Use dictionary l Why is searching through ordered list fast?  Guess a number from 1 to 1000, first guess?  What is 2 10 ? Why is this relevant? 2 20 ?  Dictionary is faster! But not ordered