Introduction to Dictionaries

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Python Dictionary.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
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.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
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.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Built-in Data Structures in Python An Introduction.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
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.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
More Python Data Structures  Classes ◦ Should have learned in Simpson’s OOP ◦ If not, read chapters in Downey’s Think Python: Think like a Computer Scientist.
10 - Python Dictionary John R. Woodward.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Now with a speaking professor! (hopefully...)
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Containers and Lists CIS 40 – Introduction to Programming in Python
Announcements Project 4 due Wed., Nov 7
Data types: Complex types (Dictionaries)
Dictionaries, File operations
Lecture 10 Data Collections
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Python
LING 388: Computers and Language
Dictionaries GENOME 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
CHAPTER THREE Sequences.
More about Numerical Computation
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Decision Structures and Indefinite Loops
Homework #5 — Monte Carlo Simulation
Elements of a Python Program
More About Functions Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Variables, Lists, and Objects
Debuggers and Debugging
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Simple Graphics Package
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
More elements of Python programs
CS150 Introduction to Computer Science 1
Notes on pyplot Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
CSV files Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Python Primer 1: Types and Operators
Note on Program Design Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Notes on Homework #6 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Nate Brunelle Today: Dictionaries
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Strings, Lists, and Files
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Dictionaries: Keeping track of pairs
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Nate Brunelle Today: Dictionaries
Numpy, pylab, matplotlib (follow-up)
Introduction to Dictionaries
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Dictionary.
Introduction to Computer Science
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Introduction to Dictionaries Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 Dictionaries

Digression — augmented assignment count = count + 1 remaining = remaining – consumption speed = speed * factor dist = dist / 2 What do these have in common? Answer:– Update of a variable by a quantity The most basic form of increment or decrement Shortcut notation:– count += 1 remaining -= consumption speed *= factor dist /= 2 Somewhat like a contraction in English CS-1004, A-Term 2014 Dictionaries

Digression (continued) Widely used notation in Python, C, C++, Java, and other languages Occasional subtle differences Example:– L[randrange(1,7)] = L[randrange(1,7)] + 1 Does not do the right thing! Should be:– idx = randrange(1,7) L[idx] = L[idx] + 1 This does work correctly in all cases:– L[randrange(1,7)] += 1 Evaluates randrange() only once! #to update correct list entry CS-1004, A-Term 2014 Dictionaries

Digression (concluded) Augmented assignment operators:– += addition -= subtraction *= multiplication /= division //= integer division %= remainder **= exponentiation And some we have not used in this class:– >>= right shift <<= left shift &= bitwise AND ^= bitwise XOR |= bitwise OR CS-1004, A-Term 2014 Dictionaries

Questions? CS-1004, A-Term 2014 Dictionaries

Dictionary Important data type in Python Read and study §11.6 And all of Chapter 11! Definition:– “Dictionary” A Python data type for collections, capable of storing and retrieving key-value pairs, … … where keys and values can be any type, … … data is unordered! Called a hash table in most other languages Not a built-in data type CS-1004, A-Term 2014 Dictionaries

Lab 5 “Write the function PlayManyGames(n) needed by Homework #5. This must call your previous function PlayOneGame(). For each call to PlayOneGame(), use the result to index into a list and increment the value stored that location in the list.” A hassle! Using indexing into list to store values not directly associated with indexes! Dice value 2  list[0] Dice value 3  list[1] … Dice value 12  list[10] CS-1004, A-Term 2014 Dictionaries

Lab 5 and HW 5 (continued) Would really like something like:– for i in range(2, 13): D[i] = 0 ... D[rollDice()] += 1 … or better yet:– D[playOneGame()] += 1 Remember that PlayOneGame() returns a pair (win/loss, # of throws) With dictionaries, we can do this! CS-1004, A-Term 2014 Dictionaries

Using Dictionaries Creating a dictionary Also called a “mapping” Remember:– Lists = square brackets — [ ] Dictionaries = curly brackets — { } Adding an item to dictionary D[key] = value key may number, string, tuple i.e., anything ‘immutable’ value can be anything at all! D[key] acts like a variable Also called a “mapping” CS-1004, A-Term 2014 Dictionaries

Getting item from Dictionary variable = D[key] Very fast, efficient “lookup” of key Uses technique in computing called “hashing” key is scrambled Scrambled value used to index into big list Easy to find by simple search from there Does not preserve order Organized internally for speed of access CS-1004, A-Term 2014 Dictionaries

Examples CS-1004, A-Term 2014 Dictionaries

Operators, functions, & methods key in dict True if key is in the dictionary, False if not dict.keys() List of keys in the dictionary dict.values() List of values in dictionary dict.items() List of (key,value) pairs dict.get(key,default) Returns dict[key] if present, default otherwise del dict[key] Deletes key and dict[key] from dictionary for k in dict: Loop over all keys in the dictionary Internal order of storage CS-1004, A-Term 2014 Dictionaries

More Examples Questions? CS-1004, A-Term 2014 Dictionaries

What can we do with dictionaries Store stuff! Easily So it is easily retrievable Organize data by different criteria Pull out non-numeric data … CS-1004, A-Term 2014 Dictionaries

Example – Homework #4 dict is a dictionary of word-count pairs Indexed by word Read file, split into words, strip, lower-case Just as in HW4 For each word of input If word in dictionary, increment dict[word] If not, dict[word] = 1 Get list of unique words uniqueWords = list(dict.keys()) Sort uniqueWords.sort() Get counts for word in uniqueWords(): write(dict[word], word) CS-1004, A-Term 2014 Dictionaries

Questions? CS-1004, A-Term 2014 Dictionaries